1 / 12

Declaring and Using Variables

Declaring and Using Variables. Visual Basic Data Types. Naming Conventions. Variables need to be assigned a data type and a name. The name should help you remember both the data type and purpose of the variable. Variable names can consist only of letters, digits, and underscores

lani-norton
Download Presentation

Declaring and Using Variables

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. Declaring and Using Variables Visual Basic Data Types

  2. Naming Conventions • Variables need to be assigned a data type and a name. • The name should help you remember both the data type and purpose of the variable. • Variable names can consist only of letters, digits, and underscores • intPopulation strCity

  3. Declaring Variables • To declare a variable you use the Dimstatement. • Syntax: • Dim variablename [As datatype] • Examples: • Dim strFname As String • Dim strLname As String • Dim strFname, strLname As String

  4. Option Explicit • To require variable declaration, include the Option Explicit statement in the General Declarations section of a form or module. • If you try to use a variable that you have not declared when Option Explicit is set, you will receive a run-time error.

  5. Conversion Functions • Val() function converts a string to a number • Dim intDegrees As Integer • intDegrees = Val(txtTemp.Text) • Str()functions converts a number to a string • lblTemp.Caption = Str(intDegrees)

  6. Calculations • ^ exponentiation • - negation • *, /multiplication and division • \ integer division • Mod modulus arithmetic • +, - addition and subtraction • Use parentheses to override the order of precedence.

  7. Assigning Values to Variables • If var is a variable, then the statement • var = expression • first evaluates the expression on the right and then assigns it’s value to the variable.

  8. Example • Private Sub cmdCaluate_Click() • Dim intNum1 As Integer • Dim intNum2 As Integer • Dim intAnswer As Integer • intNum1 = Val(txtFirstInput.Text) • intNum2 = Val(txtSecondInput.Text) • intAnswer = intNum1 ^ intNum2 • picAnswer.Print intAnswer • End Sub

  9. PictureBox Print Method • PictureBoxes can be used for output with the Print method: picAnswer.Print intNum1 picAnswer.Print intNum2 • Use a semicolon to print on the same line: picAnswer.Print intNum1; picAnswer.Print intNum2 or picOutput.Print intNum1; "+"; intNum2; "="; intAnswer

  10. PictureBox Cls Method • PictureBoxes can have their contents removed with the Cls method: picOutput.Cls

  11. Errors • Syntax errors • usually grammatical errors or misspelling picAnswer.Primt intAnswer txtAnswer.txt = intFirstInput + intSecondInput • Runtime errors • errors detected while the program is running dblAnwer = 1/intFirstInput • Logical errors • errors which occurs when the program does not run as intended dblAverage = intFirstInput + intSecondInput/2

More Related