Tuesday, April 26, 2011

Variable Declaration - 2. Implicit

To implicitly type a variable, use the corresponding suffix shown above in the data type table.  For example,

creates a string variable
TextValue$ = "This is a string"


creates an integer variable.
Amount% = 300


Do  not need declare variable before use it

Private Sub Command1_Click()
    ProName = "Book"
    Quantity = 20
    ProCode = "Al001"
    SalesPrice = 200.75
    Tax = 50
    Total = Quantity * SalesPrice + Tax
    Debug.Print "Proname", "Quantity", "SalesPrice", "Total Price"
    Debug.Print ProName, Quantity, SalesPrice, Total
End Sub

Output

Proname         Quantity         SalesPrice    Total Price
Book                20                    200.75            4065

Note

Visual Basic automatically creates a variable with that name, which you can use as if you had explicitly declared it. While this is convenient, it can lead to subtle errors in your code if you misspell a variable name.


Private Sub Command1_Click()
ProName = "Book"
      Quantity = 20
      ProCode = "Al001"
      SalesPrice = 200.75
      Tax = 50
      Total = Quantity * SalesPrice + Txa
      Debug.Print "Proname", "Quantity", "SalesPrice", "Total Price"
      Debug.Print ProName, Quantity, SalesPrice, Total
End Sub

eg.
Proname     Quantity     SalesPrice       Total
Book             20                200.75            4015

No comments:

Post a Comment