visual basic programming i 56:150 information system design

25
Visual Basic Programming I 56:150 Information System Design

Upload: brett-powell

Post on 21-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Visual Basic Programming I 56:150 Information System Design

Visual Basic Programming I

56:150 Information System Design

Page 2: Visual Basic Programming I 56:150 Information System Design

IntroductionVisual Basic has evolved from the original BASIC language and now contains several hundred statements, functions, and keywords, many of which relate directly to the Windows GUI

The Visual Basic programming system, Applications Edition included in Microsoft Excel, Microsoft Access, and many other Windows applications uses the same language. The investment you make in learning Visual Basic will carry over to these other areas.

Page 3: Visual Basic Programming I 56:150 Information System Design

Data Type• Byte 1-byte (0-255)

•Boolean 2-byte (True or False)

•Integer 2-byte (-32,768 to 32767)

•Long 4-byte (-2,147,483,648 to 2,147,483,647)

•Date 8-byte (Jan. 1, 100 to Dec. 31, 9999)

•String 0 – 65535 bytes

Page 4: Visual Basic Programming I 56:150 Information System Design

Data Type• Single 4-byte (Single-precision floating-point value)

• Double 8-byte (Double-precision floating-point value)

•Currency 8-byte (Currency value)

•Decimal Decimal value

•Variant capable of storing all system-defined types of data

Variant data can contain a special value “Null”

•object types

Page 5: Visual Basic Programming I 56:150 Information System Design

Declare Variables

To declare a variable is to tell the program about it in advance. You declare a variable with the Dim statement, supplying a name for the variable:

Dim variablename [As type] if type is not specified, it will be variant.

“dim” could be replaced by static, private, public for different purposes.

Page 6: Visual Basic Programming I 56:150 Information System Design

Variable Name:Must begin with a letter. Case Insensitive.

Can't contain an embedded period or embedded type-declaration character.

Must not exceed 255 characters.

Must be unique within the same scope, which is the range from which the variable can be referenced — a procedure, a form, and so on.

Page 7: Visual Basic Programming I 56:150 Information System Design

Implicit vs. Explicit Declaration

Function SafeSqr(num) TempVal = Abs(num) SafeSqr = Sqr(TempVal)

End Function

What if you write the second Tempval as Temval mistakenly?Option Explicit

Page 8: Visual Basic Programming I 56:150 Information System Design

Scope of Variables

Scope Private Public

Procedure-level

Variables are private to the procedure in which they appear.

Not applicable

Module-level

Variables are private to the module in which they appear.

Variables are available to all modules.

Page 9: Visual Basic Programming I 56:150 Information System Design

Constants

[Public|Private] Const constantname[As type] = expression

Const conPi = 3.14159265358979Const conMaxPlanets As Integer = 9

Const conPi = 3.14, conMaxPlanets = 9, _ conWorldPop = 6E+09 Const conPi2 = conPi * 2

Page 10: Visual Basic Programming I 56:150 Information System Design

Define fixed-size array

Follow the array name by the upper bound in parenthesis. The default lower bound is 0.

Dim Sums(20) As DoublePublic Counters(14) As Integer

The lower and upper bound can both be determined

Dim Sums(100 To 120) As String

Page 11: Visual Basic Programming I 56:150 Information System Design

Dynamic Array

Dim DynArray()

ReDim Matrix1(19, 29) ’multidimensional ’array

A dynamic array can be resized at any time.

“Preserve” can be used to keep the contents while enlarging a dynamic array.

(ReDim Preserve arrayname(dimensions)

Page 12: Visual Basic Programming I 56:150 Information System Design

Operators

Arithmetic operators +, –, *, /, mod \ integer division operator ^Exponent operator

Comparison Operators =, < >, >, < , <=, >= is result = obj1 is obj2

Page 13: Visual Basic Programming I 56:150 Information System Design

Operators

Logical operators Not, And, Or, Xor

Concatenation operators & To connect two strings.

Debug. Print “This” & “is” & “a” & “test”

Page 14: Visual Basic Programming I 56:150 Information System Design

Other Symbols

’ to make comments

_ to continue a line.

Page 15: Visual Basic Programming I 56:150 Information System Design

Decision Structure

If…ThenIf condition Then statementIf condition Then

statements End If

Page 16: Visual Basic Programming I 56:150 Information System Design

Decision Structure

If…Then…ElseIf condition1 Then

[statementblock-1][ElseIf condition2 Then

[statementblock-2]] ...[Else

[statementblock-n]] End If

Page 17: Visual Basic Programming I 56:150 Information System Design

Decision StructureSelect Case

Select Case testExpression[Case expressionlist1

[statementblock-1]][Case expressionlist2

[statementblock-2]].[Case Else

[statementblock-n]] End Select

Case else is optional

Page 18: Visual Basic Programming I 56:150 Information System Design

Loop Structure

Do...LoopDo (While | Until) condition

statementsLoop

Do statements

Loop (While | Until) condition

Page 19: Visual Basic Programming I 56:150 Information System Design

Loop Structure

For...NextFor counter = start To end [Step increment]

statementsNext [counter]

Page 20: Visual Basic Programming I 56:150 Information System Design

Sub Procedure

A Sub procedure is a block of code that is executed in response to an event.[Private|Public][Static] Sub procedurename (arguments)

statements

End Sub

The arguments for a procedure are like a variable declaration, declaring values that are passed in from the calling procedure.

Page 21: Visual Basic Programming I 56:150 Information System Design

Function Procedure

Unlike a Sub procedure, a Function procedure can return a value to the calling procedure

[Private|Public][Static]Function procedurename (arguments) [As type]

statementsEnd Function

Page 22: Visual Basic Programming I 56:150 Information System Design

Function ProcedureFunction procedures have data types, just as variables do. This determines the type of the return value. Return a value by assigning it to the procedurename itself. Function Hypotenuse (A As Integer, B As _ Integer) As String

Hypotenuse = Sqr(A ^ 2 + B ^ 2)

End Function

Page 23: Visual Basic Programming I 56:150 Information System Design

Passing arguments by valueOnly a copy of a variable is passed when an argument is passed by value. If the procedure changes the value, the change affects only the copy and not the variable itself. Use the ByVal keyword to indicate an argument passed by value.

Sub PostAccounts(ByVal intAcctNum as Integer)

statements End Sub

Page 24: Visual Basic Programming I 56:150 Information System Design

Passing arguments by reference

the variable's value can be permanently changed by the procedure to which it is passed.

Passing by reference is the default in Visual Basic.

Page 25: Visual Basic Programming I 56:150 Information System Design

References

http://msdn.microsoft.com/library/default.asp

http://graphicsmagician.com/vbcourse/index.htm