240 likes | 257 Views
Data and variables in Visual Basic. Annoucement. Lecture on Thursday 7:30PM C106 Visual Basic download: http://www.willwebs.com/vbfiles-full.exe. VB Installation. Download and run the program. Go to upzipped folder and run setup.exe. Set unzip Path. Handling the event. Get
E N D
Annoucement • Lecture on Thursday 7:30PM C106 • Visual Basic download: • http://www.willwebs.com/vbfiles-full.exe
VB Installation • Download and run the program. • Go to upzipped folder and run setup.exe Set unzip Path
Handling the event Get Text1.text Properties Text box Methods Message Dispatching Block Event Handler User Properties Button Methods Event Handler Command1_Click Print Properties Picture Box You have got clicked. Methods Event Handler
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” 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. endprint • 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: +/- 1.401298E-45 to 3.402823E+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 128 --- 10000000 (Bin), just one byte • String “128” ASCII code “1” “2” “8”, 3 bytes.
String functions • Val(string) converts “128” to 10000000. • Str(number) converts 10000000 to “128”. • Len(string) returns the length of the string (number of letters)
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 Text Box (txtGB) Label Text Box (txtMB) Picture Box (picOutput) Command Button (cmdCompute) Form
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 When clicked Declares GB variable Sets GB equal to Text in txtGB
Sub Procedure Outline • Variables • GBand 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 Begin cmdNum Declare Variables Dim GB As Single Dim Coeff As Single Input Values Calculate Total Result= GB * coeff 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