1 / 24

Data and variables in Visual Basic

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

Télécharger la présentation

Data and variables in Visual Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  3. VB Installation • Download and run the program. • Go to upzipped folder and run setup.exe Set unzip Path

  4. 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

  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” 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. endprint • 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 Text Box (txtGB) Label Text Box (txtMB) Picture Box (picOutput) Command Button (cmdCompute) Form

  18. Objects Table

  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 When clicked Declares GB variable Sets GB equal to Text in txtGB

  20. 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)

  21. Flow Charts • Define different sub procedures • Create separate paths in flowchart for different sub procedures

  22. 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

  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

More Related