Visual Basic 6 Programming. Lecture 2 : January 2005 Dr. Andrew Paul Myers 18/11/2018
Logical Variables. To define use : Dim blnTest as Boolean Assignments : blnTest = True blnTest = False Numerically 0 is False, 1 is True. 18/11/2018
Logical Expressions. A logical expression is made up of : Variables. Constants. Logical operators. e.g. some simple test conditions: intRadius = 24 intRadius < > intOld_Radius 18/11/2018
Logical Operators I. Test conditions : < less than. > greater then. < = less than or equal to. > = greater than or equal to. = equal to. < > not equal to. 18/11/2018
Logical Operators II. Logical Test Operators : And Not Or e.g. (intA <= 3) Or (intB >= 5) 18/11/2018
Conditional Statements. Conditions use logical expressions. A simple If statement : If sngX = 1.5 Then sngY = 20.5 If strText < > “Again” Then End 18/11/2018
Block If-Then-Else. If sngZ >= 3.2 Then TextBox1.Text = “3.2 or more” intI = intI + 1 blnTest = True End If If intX = 1 And intY = 2 Then intZ=3 Else intZ=2 18/11/2018
General Form. If <condition(s)> Then <statement(s)> ElseIf <condition(s)> Else <Statement(s)> End If 18/11/2018
Error Trapping. If sngRadius < 0.0 Then TextBox2.Text “Error –ve Radius!” RadiusBox.Text = “” Else sngArea = sngPi * sngRadius^2 Label1.Caption = “Area cm^2 “ TextBox1.Text = sngArea End If 18/11/2018
Case Statement. Select Case intMark Case Is >= 70 TextBox1.Text = “First!” intFirsts = intFirsts + 1 Case Is >= 60 TextBox1.Text = “2.1” intTwoOnes = intTwoOnes + 1 Case Else TextBox1.Text = “2.2 or less.” End Select 18/11/2018
Case : General Form. Select Case <variable> Case Is <condition(s)> <statement(s)> . Case Else End Select 18/11/2018
Intrinsic Functions. Functions return a value of certain data type. Parameters are passed to functions. Parameters can be variables, constants or expressions. Intrinsic means “built in”. dblY=Tan(1#) sngY=Abs(-3.5) sngY=Abs(-3.5*sngX) dblZ=dblX+3.0*Log(dblY) dblY=Val(strRadius) dblY=Log(dblX) strAnswer=Str(sngX) dblY=Sin((dblPi*degrees)/180#) 18/11/2018
See Derived Maths Functions Sheet! More examples… Sin(x) Cos(x) Tan(x) Atn(x) Sqr(x) Abs(x) Is there a Asn(x) Function? NO! Arcsin(X) = Atn(X / Sqr(-X * X + 1)) See Derived Maths Functions Sheet! 18/11/2018
Non maths functions. e.g. Convert string to number. If (IsNumeric(strText_string)) Then sngValue = Val(strText_String) End If e.g. Convert number to string. strText_String = Str(intData1) TextBox1.Text = Str(dblVolume) 18/11/2018
Aids to Program Development. Pseudo Code : “English” like version of program. Flowcharts : Graphical representation of the programs structure. Debugger : Inbuilt tool in VB, allowing users to stop and monitor the program at specified “break points”. 18/11/2018
Pseudo Code I. How to approach writing a program to solve the quadratic equation of the form : The general solution is : 18/11/2018
Pseudo Code II. Start program. Declare and initialise variables. Display a program window with titles and instructions. Do Until program finished. Enter values for a,b,c. If root is complex Then. Calculate complex roots. Else Calculate real roots. End If Display answers. Program finished? Loop. End program. 18/11/2018
Flowcharts I. Symbols : Start or end program : Process for function : Decision : Connector : Start Print Titles End? A 18/11/2018
18/11/2018
VB Debugger. Select options from the “Debug” menu. Set “Breakpoints” to halt the program. Then able to examine variable values, highlight or position cursor over variable to get value. Use “Watch points” to monitor a list of variables as the program runs. Use step options to proceed line by line or enter subroutines. 18/11/2018