Presentation is loading. Please wait.

Presentation is loading. Please wait.

Making Interactive Programs with Visual Basic .NET

Similar presentations


Presentation on theme: "Making Interactive Programs with Visual Basic .NET"— Presentation transcript:

1 Making Interactive Programs with Visual Basic .NET

2 Quick Links Using Variables Built-in data types Option Explicit
Syntax Errors The Toolbox Control

3 Using Variables A variable is a named memory location that stores a value Example: Imagine we are creating a program that takes in a user’s age as input and then displays the following messages depending on the age: If the user’s age < 18, display “Too young to vote” If the user’s age >= 18, display, “You can vote” If we are creating this program, we need to get the input and store it in a variable (memory)

4 Using Variables To get this input, we use a control object such as a TextBox control. Variables come into play when we want to store this information in memory for later use We do this in VB .NET by first declaring a variable. This is done with the following syntax: Dim age As Integer “Reserve a space in memory called ‘age’ and save enough space for an integer” Variable name

5 Variable Assignment Now that we have declared a variable, we can start using it to store Integer values using assignment age = ‘Now age stores the value 27 age = txtMyTextBox.Text ‘Now age stores the value ‘that the user entered into ‘the textbox This is wrong, however: 27 = age Good programming style to give variables meaningful names and to declare them at the beginning of the procedure

6 Some Variable Examples
A variable can only store one value at any given time An expression can also be used on the right side of an assignment statement. Remember that the expression on the right side of an assignment is evaluated first and then assigned to the variable on the left side of the = sign. Dim ageInYears As Integer Dim ageInDays As Integer ageInYears = 27 ageInDays = ageInYears * 365 ‘Not including leap years

7 Built-in Data Types A data type defines the format of data and how it is stored on a computer VB .NET has several built-in data types: Type Used to Represent Single Numbers possibly containing a decimal Double Integer Integers (no decimals) Long String A set of characters Boolean True or False

8 Variable Naming Conventions
Variable identifiers should have descriptive names and begin with an appropriate prefix as listed below Type Prefix Single sgl Double dbl Integer int Long lng String str Boolean bln

9 Naming Convention Examples
Dim strLastName As String ‘ User’s last name Dim intAge As Integer ‘ User’s age Dim dblHeight As Double ‘ User’s height in cm Dim blnVote As Boolean ‘ True if user is old ‘ enough to vote; ‘ false otherwise intAge = 17.4 ‘ ok, but doesn’t work as ‘ expected. intAge stores 17 dblHeight = ‘ dblHeight stores

10 Variable Declarations
A single Dim statement can be used to declare multiple variables of different types Dim strName As String, intAge As Integer VB .NET initializes variables when they are declared Numeric types (Double, Integer…) are initialized to 0 String variables are initialized to “” (empty string) Boolean variables are initialized to False

11 Option Explicit Dim intAge, intAgeInDays As Integer
intAg = 20 ‘ Misspelled variable – we never ‘ declared intAg intAgeInDays = intAge * 365 ‘ Gives 0 since ‘intAge was never set and was initialized to 0 by ‘VB .NET The Option Explicit statement can be added to the general section of the form module. Option Explicit displays an error message at run time when a variable is used before it is declared.

12 Syntax Errors A statement that violates the rules of VB .NET is said to contain a syntax error Example: Dim intAge As Integer = 12 ‘Syntax error! This is a syntax error because assignment is illegal in a variable declaration Syntax errors are detected by VB .NET and the error is underlined in blue in the code window Other types of errors are not detected until the program is run, at which time, a run-time error is generated

13 The TextBox Control Text boxes are used to get textual input from the user. Examples: User Name, height, age… Textbox

14 The TextBox Control TextBoxes have the properties:
Name: identifies the object and is used by the programmer. Good style to prefix with txt. Text: The text that is displayed in the text box. The user can change this text. Alignment: Sets the alignment of text relative to the text box.

15 A TextBox Control Example
TextBox to get age in Years txtAgeInYears Calculates age in days btnCalc Label that will display the result to the user lblAgeInDays

16 A TextBox Control Example
The following code goes in the event procedure for the button Dim intAgeInYears, intAgeInDays As Integer intAgeInYears = Me.txtAgeInYears.Text ‘ Get user input from ‘ text box intAgeInDays = intAgeInYears * ‘ Calc age in days Me.lblAgeInDays.Text = intAgeInDays ‘ Display the result in ‘ the label

17 Integer Division In addition to (+, -, /, *, ^), VB .NET includes integer division (\) The \ operator truncates the decimal portion of the quotient Example Dim intX As Integer intX = 20 \ 7 ‘ intX is assigned 2

18 Modulus In addition to (+, -, /, *, ^, \), VB .NET includes the Modulus operator Modulus division returns the remainder resulting from division Example Dim intX As Integer intX = 20 Mod 7 ‘ intX is assigned 6 You can use long division to find the remainder of 20 divided by 7 which is 6

19 New Order of Operations
Brackets ( () ) Exponents ( ^ ) Division ( / ) Multiplication ( * ) Integer Division ( \ ) Modulus Division ( Mod ) Addition ( + ) Subtraction ( - )

20 Automatic Type Conversion
In assignment, VB .NET automatically converts data to match the type of the variable it is being assigned to Examples Dim intX As Integer intX = ‘ intX is assigned 7 intAgeInYears = Me.txtAgeInYears.Text ‘ Assigns Text that user entered (which is ‘hopefully a number) to an Integer variable

21 Using Named Constants A constant is a named memory location which stores a value that cannot be changed at runtime from its initial assignment Example Const dblPi As Double = 3.14 dblPi = 29 ‘ This is not allowed ‘ dblPi cannot be changed ‘ at runtime


Download ppt "Making Interactive Programs with Visual Basic .NET"

Similar presentations


Ads by Google