|
Home
Visual Basic
Introduction to VB.NET
.NET Framework
VS2008 IDE
How VB is Compiled
Start Visual Studio
Windows Form App
Save Your Work
VB OOP Programming
Visual Basic Code
Exit Code
Button Event Code
Coding Recommendations
If/Then/Else
Error List Window
Comment Syntax
Help Window
Language Essentianl
Built-In Data Types
Declare Variables
Declare Constants
Code Arithmetic Expressions
Assignment Statements
Operator Precedence
Type Casting
Math Class
String Declaration
Conversion Functions
Conversion Methods
Formatting Functions
String Formatting
Variable Scope
Enumerations
Nullable Types
Loop Constructs
For Next Loop
Do While Loop
Do Until Loop
Do...Loop-While
Do...Loop-Until
Exit Do | Exit For
Do...Loop
Nested Loops
Arrays
Array Declaration
Rnd( ) Function
Listbox Control
KeyPressEventArgs
Parallel Arrays
Key Event Args
Dynamic Arrays
Redimension Array
Set Breakpoint
Start Debugger
ReDim Preserve
MultiDimensional Arrays
DataGridView Control
Length and Sort Methods
Structures
Pad Right
Split Method
IsNumeric Function
Multiform Projects
Add Form To Project
Form Object Methods
Form Show Method
ShowDialog Method
Form Close Method
Form Accept Button
Multiform Project Example
ASP.NET Web Programming
Create Data Source
Configure Access Data Source
Add Product Class
Extract Local Database Data
Order PageLoad VB Code
Add New Web Page
Set Start Page
Display Cart Aspx Code
Display Cart Design View
Sorted List Definition
VB.NET Session State
Create CartItem Class
GetCartContents Function
Add To Cart Event Handler
Remove Cart Item Event
Clear Cart Event Handler
|
A
variable stores a value that can change as the program executes. You must
declare a variable before you can use it.
The Syntax to Declare a Variable in Visual Basic
Dim VariableName [As Type] [= Expression]
|
Visual Basic Variable Declaration Examples
Dim Counter As Integer
Dim NumberOfBytes As Long = 20000
Dim InterestRate as Double = 8.125
Dim Letter As Char = "A"
Dim Line1 As String = "This is a string"
|
Summary
- A variable stores a value that can change as the program executes.
- You must declare a variable with the Dim statement before you can use it.
- You may initialize a variable when you declare it. If you do not Visual Basic assigns:
- A value of 0 to all numeric types.
- A value of false to Boolean types.
- An Empty String to String Values.
|
Figure VB-35: Visual Basic Variable Declaration Syntax and Examples
|
A
constant stores data that doesn't change during program execution. Constants
must have values assigned to them when they are declared with the
Const statement:
Visual Basic Constant Declaration Syntax:
Const Constant Name As type = Expression
|
Visual Basic Constant Declaration Examples:
Const DaysInOctober As Integer = 31
Const StateSalesTax As Double = .07
|
Visual Basic Constant Declaration Summary:
- A constant stores a value that can't be changed.
- A literal value refers to a specific numeric value like 2.5 or 1.7
|
Figure VB-36: Visual Basic Constant Declaration and Syntax
|
|
Operator
|
Name
|
Description
|
|
+
|
Addition
|
Adds two operands
|
|
-
|
Subtraction
|
Subtracts the right operand from the left operand.
|
|
*
|
Multiplication
|
Multiplies the right operand and the left operand.
|
|
/
|
Division
|
Divides the right operand into the left operand.
If both operands are integers, then the result is an integer.
|
|
\
|
Integer Division
|
Divides the right operand into the left operand returning an integer quotient.
|
|
Mod
|
Modulus
|
Returns the value that is left over after dividing the right operand into the left operand.
|
|
^
|
Exponentiation
|
Raises the left operand to the power of the right operand.
|
|
+
|
Positive Sign
|
Returns the value of the operand.
|
|
-
|
unary negative
|
Changes a positive value to a negative and vice versa.
|
Visual Basic Arithmetic Operator Summary
- An arithmetic expression consists of one or more operands and arithmetic operators.
- The first seven operators in the table above are called binary operators
because they work on two operands.
- The last two operators are called unary operators because they work on one operand.
|
Figure VB-37: Visual Basic Arithmetic Operators
|
|
Operator
|
Name
|
Description
|
|
=
|
Assignment
|
Assigns a new value to the variable.
|
|
+=
|
Addition
|
Adds the right operand to the value stored in the variable and assigns the result to the variable
|
|
-=
|
Subtraction
|
Subtracts the right operand from the value stored in the variable and assigns the result
to the variable.
|
|
*=
|
Multiplication
|
Multiplies the variable by the right operand and assigns the result to the variable.
|
|
/=
|
Division
|
Divides the variable by the right operand and assigns the result to the variable.
If the variable and the operand are both integers, then the result is an integer.
|
|
\=
|
Integer Division
|
Divides the variable by the right operand and assigns the integer quotient to the variable.
|
Visual Basic Assignment Syntax:
VariableName = Expression
|
Visual Basic Assignment Statement Examples:
Counter = 21
NewCounter = Counter
Total = Subtotal * DiscountAmount
Price = Price + .57
Price += .57
|
Visual Basic Arithmetic Assignment Summary:
- Assignment Statement: Consists of a variable, an equals sign, and an expression.
- When the Assignment Statement is executed:
- The expression is evaluated.
- The result of the operation stored in the variable.
- In addition to the equals sign, Visual Basic provides 5 additional assignment operators.
- These operators provide a shortcut assignment method.
|
Figure VB-38: Visual Basic Arithmetic Assignment Operators
|
The order in which a statement is evaluated is known as
operator precedence. Understanding operator precedence is essential
to properly coding and evaluating equations and statements.
Visual Basic Operator Order Of Precedence:
- Exponentiation.
- Positive and Negative.
- Multiplication, Division, Integer Division, Modulus.
- Addition and Subtraction.
|
Visual Basic Operator Order of Precedence Examples:
Dim DiscountPercent As Double = .2 ' 20% Discount
Dim Price As Double = 100 ' Price of $100
Price = Price * 1 - DiscountPercent ' Price = $99.80
Dim DiscountPercent As Double = .2 ' 20% Discount
Dim Price As Double = 100 ' Price of $100
Price = Price * (1 - DiscountPercent) ' Price = $80.00
|
Visual Basic Operator Order of Precedence Summary:
- Operations in an expression are evaluated from left to right in the order of precedence.
- When Parentheses are used:
- The operations in the innermost sets of parentheses are done first.
- Subsequent sets working towards the outermost sets of parentheses are done next.
|
Figure VB-39: Visual Basic Operator Order of Precedence
|
Implicit Casting Order from less Precise to more Precise Data Types:
Byte → Short → Integer → Long → Decimal → Single → Double
Char → Integer
|
Implicit Casts for Widening Conversions:
Dim Grade As Double = 92 ' Converts an Integer to Double
Dim A As Double = 6.5
Dim B As Integer = 6
Dim C As Integer = 10
Dim Ave As Double = ( A + B + C ) / 3 ' B & C & 3 will be converted to Double
' Ave = ( 22.5 / 3.0 ) = 7.5
|
Implicit Casts for Narrowing Conversions:
Dim Grade As Integer = 93.75 ' Grade = 94 due to rounding
Dim Subtotal As Double = txtSubtotal.Text ' May throw exception
Dim Average As Integer = ( A + B + C ) / 3 ' Average = 8
|
Explicit Casting with CInt and CDbl Functions:
Dim Grade As Integer = CInt( 93.75 ) ' Grade = 94 due to rounding
Dim Subtotal As Double = CDbl ( txtSubtotal.Text ) ' May throw exception
Dim Average As Integer = CInt ( CInt ( A ) + B + C ) / 3 ' Average = 7
|
Type Casting in Visual Basic Summary:
- The process of converting one data type to another is called Casting.
- A Widening Conversion transforms lower precision data to higher precision.
- Visual Basic does Widening Conversion automatically.
- A Narrowing Conversion transforms higher precision data to lower precision.
- In order to avoid exceptions - it is best to perform casting explicitly.
|
Figure VB-40: Visual Basic Type Casting Examples and Description
|
|

Help Support this site
Thank you
|
Become a consultant
Many jobs available
|
|