Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set...

9
Visual Basic Visual Basic CODE CODE

Transcript of Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set...

Page 1: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

Visual Basic Visual Basic CODECODE

Page 2: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

Basics of CodeBasics of Code

DeclarationDeclaration Set aside a named place to put thingsSet aside a named place to put things

AssignmentAssignment Put things in a named placePut things in a named place

ControlControl Interrupt the “top-to-bottom” flow of the Interrupt the “top-to-bottom” flow of the

programprogram Subroutines and FunctionsSubroutines and Functions

Write code once and invoke it from many Write code once and invoke it from many placesplaces

Page 3: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

DeclarationDeclaration

Declares a space (like a mailbox slot) Declares a space (like a mailbox slot) for storage of data.for storage of data.

Specify the Specify the namename of the of the variablevariable, and , and the the typetype it is. For now, consider types it is. For now, consider types of Integer, Boolean, String, of Integer, Boolean, String, Single/Double.Single/Double.

Syntax: Syntax: DIM name AS typeDIM name AS type

DIM lastButton AS IntegerDIM lastButton AS Integer

DIM myResponse AS StringDIM myResponse AS String

Page 4: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

AssignmentAssignment

Assign a value or expression to a Assign a value or expression to a storage place.storage place.

The storage place can be a simple The storage place can be a simple variable, or a property of a control.variable, or a property of a control.

VariableVariable = = expressionexpression

command1.visible = Falsecommand1.visible = False

lastButton = 2lastButton = 2

Matches = (lastButton = 1) AND Matches = (lastButton = 1) AND (thisButton <> 0)(thisButton <> 0)

Page 5: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

ControlControl

Program normally proceeds from line to line.Program normally proceeds from line to line. Control statements cause other parts of code Control statements cause other parts of code

to run.to run.

IF IF expressionexpression THEN THEN

code to run if ‘expression’ not 0code to run if ‘expression’ not 0

ELSEELSE

code to run if ‘expression’ is 0code to run if ‘expression’ is 0

END IFEND IF

Page 6: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

ControlControl

Example:Example:IF lastButton <> 0 THENIF lastButton <> 0 THEN command1.Visible = Falsecommand1.Visible = FalseEND IFEND IFIF lastButton THENIF lastButton THENthisButton = lastButtonthisButton = lastButton

ELSEELSEcommand1.caption = “First Pressed”command1.caption = “First Pressed”

END IFEND IF

Page 7: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

ControlControl Other control mechanismsOther control mechanismsFOR FOR variablevariable = = startValstartVal TO TO endValendValNEXT NEXT variablevariable

DO WHILE DO WHILE expressionexpressionLOOPLOOP

DODOLOOP WHILE LOOP WHILE expressionexpression

DODOLOOP UNTIL LOOP UNTIL expressionexpression

Page 8: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

‘‘Sub’ routinesSub’ routines Put several lines of code in a ‘sub’ and Put several lines of code in a ‘sub’ and

then you can execute all of that code with then you can execute all of that code with one line:one line:

Sub MySub()Sub MySub()Command1.Picture = LoadPicture(“”)Command1.Picture = LoadPicture(“”)Command2.Picture = LoadPicture(“”)Command2.Picture = LoadPicture(“”)Command3.Picture = LoadPicture(“”)Command3.Picture = LoadPicture(“”)

End SubEnd SubPrivate Sub Command1_Click()Private Sub Command1_Click() call MySubcall MySubEnd SubEnd Sub

Page 9: Visual Basic CODE. Basics of Code Declaration Declaration Set aside a named place to put things Set aside a named place to put things Assignment Assignment.

FunctionsFunctions

Subroutines that return a value.Subroutines that return a value.Public Function YtoM(Y AS Single) Public Function YtoM(Y AS Single) AS SingleAS Single

YtoM = Y * 0.9YtoM = Y * 0.9End FunctionEnd Function……Yards = 10Yards = 10Yards = YtoM(Yards)Yards = YtoM(Yards)