Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic Programming I 56:150 Information System Design.

Similar presentations


Presentation on theme: "Visual Basic Programming I 56:150 Information System Design."— Presentation transcript:

1 Visual Basic Programming I 56:150 Information System Design

2 Introduction Visual 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.

3 Data Type Byte1-byte (0-255) Boolean2-byte (True or False) Integer 2-byte ( -32,768 to 32767) Long4-byte ( -2,147,483,648 to 2,147,483,647) Date8-byte (Jan. 1, 100 to Dec. 31, 9999) String0 – 65535 bytes

4 Data Type Single4-byte (Single-precision floating-point value) Double8-byte (Double-precision floating-point value) Currency8-byte (Currency value) DecimalDecimal value Variant capable of storing all system-defined types of data Variant data can contain a special value “Null” object types

5 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.

6 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.

7 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

8 Scope of Variables ScopePrivatePublic 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.

9 Constants [Public|Private] Const constantname[As type] = expression Const conPi = 3.14159265358979 Const conMaxPlanets As Integer = 9 Const conPi = 3.14, conMaxPlanets = 9, _ conWorldPop = 6E+09 Const conPi2 = conPi * 2

10 Define fixed-size array Follow the array name by the upper bound in parenthesis. The default lower bound is 0. Dim Sums(20) As Double Public Counters(14) As Integer The lower and upper bound can both be determined Dim Sums(100 To 120) As String

11 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)

12 Operators Arithmetic operators +, –, *, /, mod \ integer division operator ^Exponent operator Comparison Operators =,, >, = is result = obj1 is obj2

13 Operators Logical operators Not, And, Or, Xor Concatenation operators & To connect two strings. Debug. Print “This” & “is” & “a” & “test”

14 Other Symbols ’ to make comments _ to continue a line.

15 Decision Structure If…Then If condition Then statement If condition Then statements End If

16 Decision Structure If…Then…Else If condition1 Then [statementblock-1] [ElseIf condition2 Then [statementblock-2]]... [Else [statementblock-n]] End If

17 Decision Structure Select Case Select Case testExpression [Case expressionlist1 [statementblock-1]] [Case expressionlist2 [statementblock-2]]. [Case Else [statementblock-n]] End Select Case else is optional

18 Loop Structure Do...Loop Do (While | Until) condition statements Loop Do statements Loop (While | Until) condition

19 Loop Structure For...Next For counter = start To end [Step increment] statements Next [counter]

20 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.

21 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] statements End Function

22 Function Procedure Function 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

23 Passing arguments by value Only 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

24 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.

25 References http://msdn.microsoft.com/library/default.asp http://graphicsmagician.com/vbcourse/index.htm


Download ppt "Visual Basic Programming I 56:150 Information System Design."

Similar presentations


Ads by Google