Data and variables in Visual Basic
Annoucement Lecture on Thursday 7:30PM C106 Visual Basic download:
VB Installation Download and run the program. Go to upzipped folder and run setup.exe Set unzip Path
Handling the event Message Dispatching Block User Text box Properties Methods Event Handler Button Properties Methods Event Handler Command1_Click Picture Box Properties Methods Event Handler Print Get Text1.text You have got clicked.
Define other event handler When you double click the command button, you only define the default event handler (command1_click). You can define other event as well. Demo
Variable Variables are storage unit of data. Distance = Speed * Time You need to declare a variable before using it. For example: Dim Distance as Integer … Distance = … = reads “is assigned with” Distance500 Speed100 Time5 In memory
Two meanings of “=“ sign Assignment operator: assign the value of the RIGHT side to the LEFT side. Dim A as Integer A=0 A=A+1 Relational operator If A = B Then Reads “A is assigned with A+1” Reads “If A equals to B then”
Doing Stuff with Variables Equations: Answer always on LEFT Total = Weight * Coeff Math +, - * for multiplication / for division ^ for exponent: volume = width^3 ( ) for separating parts Numerical functions: Sqr(), Int(), Round(), sin()
Order of Operations Parentheses Exponents Multiplication and Division Addition and Subtraction JUST LIKE ALGEBRA!
Naming a variable Start with a letter Followed by letter or number or underscore. Less than 255 letters Symbols that are forbidden in a variable name: Almost every special symbol on keyboard. Certain key word is reserved and can not be used as variable name, e.g. end print Right: A_2, Speed, Wrong: 2Heavy, private, Weight-factor
Review of Variables Different Types: Byte, Integer, Long, Single, String Naming- use descriptive names Input and output – do not put variable names inside quotes
Data types in VB Numeric data (and their range) Byte: 0 ~ 255 Integer: -32,768 to 32,767 Long: -2,147,483,648 to 2,147,483,648 Single: +/ E-45 to E+38 String A group of ASCII symbols. Each symbol takes one byte.
Strings String data is defined between two double quotations. “Hello world” String variable is a name used to refer to a string. String1=“Hello world” Difference between a number and a string of a number. Number (Bin), just one byte String “128” ASCII code “1” “2” “8”, 3 bytes.
String functions Val(string) converts “128” to Str(number) converts to “128”. Len(string) returns the length of the string (number of letters)
Building a Program
Outlining a Program Draw the form Define the objects in the form Define key properties Define which objects have sub procedures
Sample Outline – GB to MB Picture Box (picOutput) Text Box (txtGB) Command Button (cmdCompute) Label Form Text Box (txtMB)
Objects Table ObjectTypePropertiesEvent handler txtGBTextBox txtMBText Box cmdComputeCommand Button Click() Label1, Label 2, … Labels picOutputPicture Box Form1Form
Getting Number Values into Variables Variable = Val(TextBox.Text) We have a textbox named txtGB, command button named cmdNum Private Sub cmdNum_Click() Dim GB As Single GB = Val(txtGB.Text) … End Sub Declares GB variable Sets GB equal to Text in txtGB When clicked
Sub Procedure Outline Variables GB and Coeff are Single from textbox GB = Val(txtGB.Text) result is Single to be calculated Equations Result= GB * Coeff Output: Total to Answer (PictureBox)
Flow Charts Define different sub procedures Create separate paths in flowchart for different sub procedures
GB to MB Calculator Input Values Calculate Total Result= GB * coeff Begin cmdNum Declare Variables Dim GB As Single Dim Coeff As Single Output Total End cmdNum
Variables, Calculations, and Output Private Sub cmdCompute_Click() Dim GB As Single Dim coeff As Single Dim result As Single GB = Val(txtGB.Text) coeff = Val(txtMB.Text) result = GB * coeff picOutput.Print result; "MBs" End Sub
Comments in Program Private Sub cmdCompute_Click() ‘This program will convert GB to MB ‘declare GB, coeff, result Dim GB As Single Dim coeff As Single Dim result As Single ‘convert input from string to number GB = Val(txtGB.Text) coeff = Val(txtMB.Text) ‘computer result result = GB * coeff ‘output the result picOutput.Print result; "MBs" End Sub