Download presentation
Presentation is loading. Please wait.
Published byLuke Gibbs Modified over 8 years ago
1
Data and variables in Visual Basic
2
Annoucement Lecture on Thursday 7:30PM C106 Visual Basic download: http://www.willwebs.com/vbfiles-full.exe http://www.willwebs.com/vbfiles-full.exe
3
VB Installation Download and run the program. Go to upzipped folder and run setup.exe Set unzip Path
4
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.
5
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
6
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
7
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”
8
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()
9
Order of Operations Parentheses Exponents Multiplication and Division Addition and Subtraction JUST LIKE ALGEBRA!
10
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
11
Review of Variables Different Types: Byte, Integer, Long, Single, String Naming- use descriptive names Input and output – do not put variable names inside quotes
12
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.
13
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.
14
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)
15
Building a Program
16
Outlining a Program Draw the form Define the objects in the form Define key properties Define which objects have sub procedures
17
Sample Outline – GB to MB Picture Box (picOutput) Text Box (txtGB) Command Button (cmdCompute) Label Form Text Box (txtMB)
18
Objects Table ObjectTypePropertiesEvent handler txtGBTextBox txtMBText Box cmdComputeCommand Button Click() Label1, Label 2, … Labels picOutputPicture Box Form1Form
19
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
20
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)
21
Flow Charts Define different sub procedures Create separate paths in flowchart for different sub procedures
22
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
23
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
24
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.