Two VB Programs - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Two VB Programs

Description:

Comments may appear at the end of an executable line or on their own line ... Dim time As Date = #1:45:00 PM# Constants. Constants are declared with the Const keyword ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 17
Provided by: davidb195
Category:
Tags: date1 | programs | two

less

Transcript and Presenter's Notes

Title: Two VB Programs


1
Two VB Programs
  • "Hello World" in a console
  • "Hello World" in a form

2
The Source Code - Console
  • "Hello World" console

Module Module1 Sub Main()
Console.WriteLine("Hello World")
Console.ReadLine() End Sub End Module
3
The Source Code - Form
  • We can't see the source code
  • We can see the label properties with a dialog box
  • We can add source code to act on an event to a
    widget or form

4
Comments
  • Comments may appear at the end of an executable
    line or on their own line
  • Comment early, comment often

' Print the message. Console.WriteLine( "Hello
World" ) Console.ReadLine() ' Wait for input
before closing window
5
Data Types
  • VB is a typed language, so all variables,
    constants, and return values must be assigned a
    type
  • Some primitive types areInteger, Long, Single,
    Double, Char, String, Boolean, Date, Decimal

6
Data Type Values
  • Boolean (True or False)
  • Char (one Unicode character)
  • Date (date and time - m/d/y hms format)
  • Decimal (for monetary calculations)
  • Integer (32 bit integer)
  • Long (64 bit integer)
  • Single (32 bit float)
  • Double (64 bit float)
  • String (up to 2 billion Unicode characers)

7
Declaring Variables
  • Variables are defined with the Dim (Dimension)
    keyword
  • Variables must be assigned a type when declared
  • Multiple variables may be assigned at once
  • Numbers have default value of 0, strings to the
    empty string, booleans to False

8
Declaring Variables (Cont'd)
  • Names must start with a letter or the underscore
  • The rest of the name may have letters, digits, or
    the underscore
  • Names cannot contain spaces or periods
  • Names cannot be a VB keyword
  • Variables must be declared before they can be used

9
Sample Variable Declarations
  • Dim x As Integer
  • Dim a, b, c As Double
  • Dim s As String
  • Dim d as Date
  • Dim yn as Boolean

10
Assigning Values
  • Variables may be assigned values only after they
    are declared
  • A single variable may be assigned a value upon
    declaration
  • The assigned value must be of the proper type

11
Sample Value Assigning
  • Dim x As Integerx 5
  • Dim q As Double 5.0
  • Dim s As String "Hello World"
  • Dim ch As Char "X"c('c' denotes a character)
  • Dim today As Date 5/1/2008
  • Dim time As Date 14500 PM

12
Constants
  • Constants are declared with the Const keyword
  • Constants must be assigned a value when they are
    declared
  • Constants should stand out from variables, and
    are usually given names in all caps

13
Sample Constants
  • Const Q As Integer 5
  • Const RATE As Double 5.5
  • Const ERR As String "Error"
  • Const YES As Boolean True

14
Naming Prefixes
  • Variables and constants can use any name that is
    not a keyword
  • Some books recommend the use of naming prefix - a
    prefix that makes clear the type of variable
  • Their usage is optional, but can be helpful

15
Sample Naming Prefixes
  • Boolean bln - blnFlag
  • Char chr - chrInputLetter
  • Date dat or dtm - datToday
  • Decimal dec - decCash
  • Integer int - intLength
  • Long lng - lngBigValue
  • Single sng - sngWeight
  • Double dbl - dblDistance
  • String str - strName

16
Variable Scope
  • Variables declared inside a method are local
    variables and are visible only inside that
    procedure
  • Variables declared inside a class but outside a
    method are class-level variables and can be used
    anywhere in the class
  • Variables declared outside of a class or method
    are global variables
Write a Comment
User Comments (0)
About PowerShow.com