Visual Basic Introduction n Visual Studio 6 0

Visual Basic

Introduction n Visual Studio 6. 0: Collection of Microsoft Visual development applications (Visual C++, Visual J++, Visual Basic, etc…) n VB is a programming language. 6 Versions launched since its creation in the 90’s. n “Easy and powerful tool for developing Windows applications in Basic. ” Bill Gates.

VB Development Environment n n Integrated Development Environment (IDE): visual environment to develop, run, test and debug. Controls: generally visual, and readymade components, assigned a set of properties (I. e. Text Box, Button, List Box, etc…). Intelli. Sense: Microsoft’s sophisticated completion technology. It simplifies the coding process. Event driven programming: flow of the application dictated by the user actions

VB IDE Toolbar menu bar Project Explorer Toolbox Immediate window Properties window Form layout

VB IDE Components n n n Menu Bar: contains all commands needed to run VB (File, Edit, View etc…). Toolbars: quick access to commonly used menu commands. Project Explorer: displays the project’s components. Toolbox: contains icons of the controls that can be placed on the project’s Forms to create the application’s interface. Properties Window: Shows the property settings of the selected control (size, caption, color, etc…).

VB IDE Components (Cont’d) n Form Designer: Main window in which one can design and edit the application’s user interface. In this window the user can enter and edit the application’s code. It displays two windows: Form and Code window. n Form Layout: Shows the initial positions of the forms in the application. It is useful in multiple forms applications. n Immediate Window: Debugging tool.

Programming Steps n Step 1: Customize the windows that the user sees. I. e. placing controls and components on the layouts of the project’s Forms. n Step 2: Decide on the events each control should recognize. n Step 3: Coding the event procedures for those events.

Variable Declaration n Visual Basic code works in two modes: – – n Implicit: does not require variable declaration Explicit: requires variable declaration Declare variables using Dim Variable. Name As Data. Type Example: Dim length As Integer Dim greetings As String

Variable Types n n n Numeric: stores numbers String: stores text Variant: stores any type of data Boolean: true/false Date Object

Implicit/Explicit n Implicit Example Public Sub VBImplicit() x=5 y = “Hello” End Sub n n Explicit Example Start code window by: Option Explicit […] Public Sub VBExplicit() Dim x As Integer, y As String x=5 y = “Hello” End Sub Implicit: default VB mode, x and y stored as variants Explicit: x can only store integers, y only strings ALWAYS use explicit mode, it reduces errors

Variable Scope Code Form 1 Variables Sub 1 Variables . . . Sub 2 Variables Code Form 2

Variable Scope (cont’d) n n The same variable x is needed in both subroutines and therefore is defined as a global variable (I. e. declaration outside subs) There is a different local variable y for each subroutine (I. e. declaration inside subs) Dim x As Integer Public Sub First. Sub() x=5 Dim y As Integer End Sub Public Sub Second. Sub() x=6 Dim y As String End Sub

Variable Generalities n n n 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 int. Length As Integer – String: str… – Double: dbl… – Text Control: txt – Etc.

Constants n n Constants do not change their value during the execution of the program. Declaration: Constant. Name As Data. Type Public Const pi As Double = 3. 14159265358979

Arrays n n Arrays hold a set of related data. Declaration: Dim Array. Name(Array. Size) As Data. Type Dim str. Names(15) As String or Dim str. Names(1 To 16) As String str. Names is an array that holds 16 string values. n Multidimensional arrays: Dim dbl. Temperature(99, 99) As Double

Dynamic Arrays n Size not defined at the beginning of the program: Dim dbl. Matrix() as Double n You can re-dimension the array once you know the size of the array: Re. Dim dbl. Matrix(User. Count, User. Count)

What’s Next Basic VB syntax n Functions and Subroutines n Common VB controls n
- Slides: 17