Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare.

Similar presentations


Presentation on theme: "Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare."— Presentation transcript:

1 Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare a variable with a Dim statement Dim VariableName [As DataType ] Variable declared with the Dim statement within a procedure exists only as long as the procedure is executing

2 Variables,Constants and Data types When the procedure is completed, the value of the variable disappears The value of the variable in a procedure is Local to that procedure A variable name –must begin with a letter –must not exceed 255 characters

3 Variables,Constants and Data types Data types are –Numeric data types Integer32767 -32768 Long2,147,xxx,xxx Single 3.4*10**38 Double1.8*10**308 Currency999999999999999.9999 Example Dim Amnt as Double, (Prev,Temp) as Integer Dim NameAndLName As String

4 Variables,Constants and Data types The String Data type –String type data type can be declared as Max length of a string variable can 32767 Dim Name As string * 24 ‘Fixed length’ Dim Adress As string ‘Variable size’

5 Variables,Constants and Data types The Byte Data Type If the variable contains binary data, Declare it as an array of the Byte data type. Dim Temp(1 to 100) As Byte;

6 Variables,Constants and Data types The Boolean Data type If you have variable that will contain true/false, or on/off information, you can declare it to be of type Boolean Dim Flag As Boolean

7 Variables,Constants and Data types The Variant Data type A variant is capable of storing all system- defined types of data. You don’t have to convert between these types of data. Dim SomeValue‘Variant by default ‘

8 Variables,Constants and Data types The Object Data type Object variables are stored as 32-bit addresses. A variable declared as Object is one that can be assigned(using the set Statement) to refer to any actual object. Dim ObjDb As Object Set ObjDb =OpenDatabase(“c:\vb5\Biblio.mdb”)

9 Variables,Constants and Data types Converting Data Types Visual Basic Provides several conversion functions you can use to convert values into a specified data type ConversionConverts anConversionConverts an Functionexpression toFunctionExpression to CboolBooleanCintInteger CbyteByteCLngLong CcurCurrencyCSngSingle CdateDateCStrString CDblDoubleCvarVariant

10 Variables,Constants and Data types There are other ways to declare variables –Declaring a variable in the declaration section of a Form, Standard module, or Class module, rather than within a procedure, makes the variable available to all the procedures in the module –Declaring a variable using Public keyword makes it available throughout your application –Declaring a variable using the Static keyword preserves its value even when a procedure ends.

11 Variables,Constants and Data types To avoid the problem of misnaming variables place Option explicit –in the declaration section of a class, form, or standard module –OR –From Tools(Options(Editor(RequireVariableDeclarati on))

12 Array Declaration Array allow you to refer to a series of variables by the same name and to use a number(an index) to tell them apart All the elements in an array have the same data type –Fixed-size arrays –Dynamic arrays(Can change at run-time)

13 Array Declaration Declaring Fixed size arraysUsing Dim Statement Dim Counter(20) As Integer ‘ 21 elements’ Dim Sums(100 To 120) As Double Dim Multi(3, 1 To 10, -40 To 50) As Long LBound and Ubound functions returns upper and loower limits of a given dimension For I = LBound(Multi,3) To UBound(Multi,3) -40 50

14 Array Declaration There are 3 ways to declara fixed size arrays –To create Public arrays, use Public statement in the declaration section of a module –To create a Module-level array, use the private statement in the declaration section of a module –To create a local array, use the Private statement in a procedure

15 Array Declaration Dynamic Arrays –Using either Public, Dim, Static –First declare the array declaration Dim TempArr() –Then using Redim statement Redim TempArr(X+1)

16 Control Structures Decision Structures Visual basic Procedures can test conditions and then, depending on the results of that test, perform different operations. If.. Then If.. Then.. Else Select Case

17 Control Structures If.. Then Structure If condition Then Statement If condition Then Statements endif

18 Control Structures If.. Then.. Else Structure If condition Then [Statementblock-1] [ElseIf condition2 Then [statement-2]] … [Else [Statemnt-n]] Endif

19 Control Structures Select Case statement Select Case testexpression [Case expressionlist1 [statemntblock-1]] [Case expressionlist2 [statement-2]].. Case else [statementblock-n]] end select

20 Control Structures Loop Structure Loop structure allow you to execute one or more lines of code repetitively –Do.. Loop –For.. Next –For Each.. Next

21 Control Structures Do.. Loop –Test First Condition Do [While / Until] Condition Stataments Loop

22 Control Structures –Test Last Condition Do Statements Loop [While/Until] condition

23 Control Structures For.. Next condition For Counter = start To end [Step increment] statements Next[counter]

24 Control Structures For Each…Next It repeats a group of statements for each element in a collection of objects or in an array instead of repeating the statement a specified number of times For Each elemen In Group statements Next [element]

25 Scoping Variables Depending on how it is declared, a variable is scoped as either a procedure-level or module-level

26 Exiting From Loops Exiting from loops Ubnormal exiting from loops –Exit For –Exit Do –Exit Sub

27 String Functions –Len(a) :Returns the length of a string –Mid(a1,Start,length) :returns a specified number of characters(length), from a string a1, starting position of Start. –Instr(Start,a1,a2) :Returns the first occurrences of one string(a2), within another string(a1), starting(Start) position of a2

28 String Functions Left(a1,a2) :Returns a specified(a2) number of characters from the left of string(a1) Right(a1,a2) :Returns a specified(a2) number of characters from the Right of string(a1) Str(a1) : Returns a string representation of a number a1

29 Examples Select Case Asc(userInput) Case 65 To 90 Msg = “You entered the uppercase letter” Msg=Msg&Chr(Asc(UserInput))&”.” Case 97 To 122 Msg =“You entered the lower case letter “ Msg=Msg&Chr(Asc(UserInput))&”.” Case 1, 3, 5 Msg=“You entered odd number” Case else Msg=“You did not enter a letter or a number.” End Select

30 Example You can use multiple expressions or ranges in each Case clause Case 1 To 4, 7 To 9, 11, 13 Case is > MaxNumber Case “everything”,”nuts” To “soup”, TestIteem

31 Gosub..Return Statement Branch to, and return from A subroutine within a procedure Sub Form_Click() Dim Num Num = InputBox(“Type A Number”) Gosub Routine Goto NextPart Routine: Num = Num / 2 Return NextPart : MsgBox “Half Of Your Number is “ & Num End Sub

32 Examples For I = 1 To 10 For j = 1 to 10 … Next j Next I Nl = Chr(13) & Chr(10) For Rep = 5 To 1 Step - 1 For I = Asc(“A”) To Asc(“Z”) Msg = Msg & Chr(I) Next i Msg = Msg & Nl Next Rep MsgBox Msg


Download ppt "Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare."

Similar presentations


Ads by Google