1.a. vb lecture 1
TRANSCRIPT
CVEV 118/698CVEV 118/698 Visual Basic Visual Basic
Lecture 1Lecture 1
Prof. Mounir MabsoutProf. Mounir MabsoutExpert 1: Elsa SulukdjianExpert 1: Elsa SulukdjianExpert 2: Walid El AsmarExpert 2: Walid El Asmar
IntroductionIntroduction
Visual Studio 6.0: Collection of Microsoft Visual Studio 6.0: Collection of Microsoft Visual development applications (Visual Visual development applications (Visual C++, Visual J++, Visual Basic, etc…)C++, Visual J++, Visual Basic, etc…)
VB is a programming language. 6 VB is a programming language. 6 Versions launched since its creation in Versions launched since its creation in the 90’s.the 90’s.
““Easy and powerful tool for developing Easy and powerful tool for developing Windows applications in Basic.” Bill Windows applications in Basic.” Bill Gates.Gates.
VB Development VB Development EnvironmentEnvironment
Integrated Development Environment (IDE): Integrated Development Environment (IDE): vvisual environment to develop, run, test and isual environment to develop, run, test and debug.debug.
Controls: Controls: generally visual, and readymade generally visual, and readymade components, assigned a set of properties (I.e. components, assigned a set of properties (I.e. Text Box, Button, List Box, etc…). Text Box, Button, List Box, etc…).
IntelliSense: IntelliSense: Microsoft’s sophisticated Microsoft’s sophisticated completion technology. It simplifies the coding completion technology. It simplifies the coding process.process.
Event driven programming:Event driven programming: f flow of the low of the application dictated by the user actions (click application dictated by the user actions (click of a mouse, keystrokes, etc…).of a mouse, keystrokes, etc…).
VB IDEVB IDEmenu bar
Toolbar
Project Explorer
Properties window
Form layout
Toolbox
Immediate window
VB IDE ComponentsVB IDE Components Menu Bar:Menu Bar: contains all commands needed to contains all commands needed to
run VB (File, Edit, View etc…).run VB (File, Edit, View etc…). Toolbars:Toolbars: quick access to commonly used quick access to commonly used
menu commands.menu commands. Project Explorer:Project Explorer: displays the project’s displays the project’s
components.components. Toolbox: Toolbox: contains icons of the controls that contains icons of the controls that
can be placed on the project’s Forms to create can be placed on the project’s Forms to create the application’s interface.the application’s interface.
Properties Window: Properties Window: Shows the property Shows the property settings of the selected control (size, caption, settings of the selected control (size, caption, color, etc…).color, etc…).
VB IDE Components VB IDE Components (Cont’d)(Cont’d)
Form Designer:Form Designer: Main window in which Main window in which one can design and edit the application’s one can design and edit the application’s user interface. In this window the user user interface. In this window the user can enter and edit the application’s code. can enter and edit the application’s code. It displays two windows: Form and Code It displays two windows: Form and Code window.window.
Form Layout:Form Layout: Shows the initial positions Shows the initial positions of the forms in the application. It is of the forms in the application. It is useful in multiple forms applications.useful in multiple forms applications.
Immediate Window:Immediate Window: Debugging tool. Debugging tool.
Programming StepsProgramming Steps
Step 1: Step 1: Customize the windows that Customize the windows that the user sees. I.e. placing controls the user sees. I.e. placing controls and components on the layouts of and components on the layouts of the project’s Forms.the project’s Forms.
Step 2: Step 2: Decide on the events each Decide on the events each control should recognize.control should recognize.
Step 3:Step 3: Coding the event Coding the event procedures for those events. procedures for those events.
Variable DeclarationVariable Declaration
Visual Basic code works in two Visual Basic code works in two modes:modes:– Implicit:Implicit: does not require variable does not require variable
declarationdeclaration– Explicit:Explicit: requires variable declaration requires variable declaration
Declare variables usingDeclare variables usingDim Dim VariableNameVariableName As As DataTypeDataType
Example:Example: Dim length As IntegerDim length As Integer
Dim greetings As StringDim greetings As String
Variable TypesVariable Types
Numeric: Numeric: stores numbers String: stores text Variant: stores any type of data Boolean: true/false Date Object
Implicit/ExplicitImplicit/Explicit Implicit ExampleImplicit Example Public Sub VBImplicit()
x = 5y = “Hello”
End Sub
Explicit ExampleExplicit ExampleStart code window by: Option Explicit […] Public Sub VBExplicit() Dim x As Integer, y As
String x = 5 y = “Hello”
End Sub Implicit: Implicit: default VB mode, x and y stored default VB mode, x and y stored
as variantsas variants Explicit: Explicit: x can only store integers, y only x can only store integers, y only
stringsstrings ALWAYS use explicit mode, it reduces ALWAYS use explicit mode, it reduces
errorserrors
Variable ScopeVariable Scope
Sub1Sub1 Variables
Form 1
Sub2Sub2 Variables
...
Form1 Variables
Code
Form 2
Code
Variable Scope (cont’d)Variable Scope (cont’d)
Dim x As Integer
Public Sub FirstSub()x = 5 Dim y As Integer
End Sub
Public Sub SecondSub()
x = 6Dim y As String
End Sub
The The samesame variable variable x x is needed in both is needed in both subroutines and subroutines and therefore is defined therefore is defined as a as a global variable global variable (I.e. declaration (I.e. declaration outside subs)outside subs)
There is a different There is a different locallocal variable variable yy for for each subroutine (I.e. each subroutine (I.e. declaration inside declaration inside subs)subs)
Variable GeneralitiesVariable Generalities Variable names need to be significant for
clear coding practice. Don’t be shy of using long names. Usually the first character(s) indicate the
variable type (e.g):– Integer: int… Dim intLength As
Integer– String: str…– Double: dbl…– Text Control: txt– Etc.
ConstantsConstants
Constants do not change their value during the execution of the program.
Declaration: Const ConstantName As DataTypePublic Const pi As Double =
3.14159265358979
ArraysArrays
Arrays hold a set of related data. Declaration:
Dim ArrayName(ArraySize) As DataTypeDim strNames(15) As String
or Dim strNames(1 To 16) As String
strNames is an array that holds 16 string values.
Multidimensional arrays:Dim dblTemperature(99,99) As Double
Dynamic ArraysDynamic Arrays
Size not defined at the beginning of the program:Dim dblMatrix() as Double
You can re-dimension the array once you know the size of the array:ReDim dblMatrix(UserCount,UserCount)
What’s NextWhat’s Next
Basic VB syntaxBasic VB syntax Functions and SubroutinesFunctions and Subroutines Common VB controlsCommon VB controls