basically you can write this expression using a combination of if and else statements, but using the...
TRANSCRIPT
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
1/16
visual Basic .NET language Quick Overview
Despite its ease of use, VB.NET is cable of doing all the things you want like component-based
programming using object oriented programming (OOP). You can define new types, you can create new
classes and their respective properties and methods. Your classes can incorporate OOP features like
inheritance, polymorphism and encapsulation.
So in summary, VB.NET can: define new types (classes), class properties and methods, inheritance,
polymorphism, encapsulation, interfaces, structures, events, attributes etc.
Hello Word!
Console Application
Module YourFirstVBProgram
Sub Main()
System.Consoloe.WriteLine(Hello, World!)
End Sub
END Module
ASP.NET Page
Response.Write(Hello, World!)
Fundamentals
Types
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
2/16
There are built in types like Boolean and Integer, and there are custom types that you can create
yourself like classes.
For numerical values, you can use the following types: Integer, Short or Long. If you require more
precision use: Single, Double or Decimal. For a single character use: Char.
Many times you want to assign the value of one type to another type, this is commonly referred to as
Casting.
An example would be assigning a variable of type Integer to another variable of type Double. Since all
values of an integer can be held by a Double type, there is no conversion necessary. This is sometimes
referred to as a widening conversion as oppose to a narrowing conversion like when you assign a
Integer value to a Short type. The difference between the two is that a widening conversion is done
automatically i.e. you dont have to do anything special whilst coding. On the other hand, a narrowing
conversion will require you to cast the value (Note: if you have Option Explicit set to off you will not
have to do this, a potential for danger!).
Explicit Cast Example
Dim dlbNumberOfDaysInAYear As Double = 183.5
Dim iMyAge = CType(dlbNumberOfDaysInAYear, Integer) explicit cast
Explicit Casting Methods include: CBool(), CByte(), CChar(), CDate(), CDbl(), CDec(), Dint(), CLng(), CObj(),
CShort(), CStr(), CStr(), CType()
Identifiers
An identifier is simply the name you choose for your variables, objects etc. In VB.NET, your identifier
must begin with either a letter or underscore ('_'). If your coming from a C# background, keep in mind
that identifiers are NOT case sensitive, so both strUserName and strusername are the same thing!
Variables and Constants
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
3/16
A variable is declared by first typing the type, following by the name you want to call the variable.
Variables will be defaulted to a defaulted value if you fail to initialize the variable.
# All numeric types will be assigned to 0.
# Date type will be given the value of 12:00:00AM 1/1/1001
# Boolean type is False.
# Char type is defaulted to ''.
# String values are "".
The following is an example of declaring an Integer type variable and initializing it to the value of 7,
following by assigning it a new value of 15.
Dim iUserAge As Integer = 7
Integer = 15
To make a variable a constant (a variable whos value cannot change) you simply do:
Const iUserAge As Integer = 7
Enumerations
An enumeration allows you to group a list of named constants, whos type can be either Integer (default
type), Short or Long.
Public Enum CarManufacturers
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
4/16
Honda = 1
Ford = 2
Toyota = 3
Ferrari = 5
Nissan = 6
End Enum
The default number that an enumeration starts off is 0 unless you explicity assign a different value like I
did above.
To output the value of an enumeration you must cast to the type you chose, example:
Response.Write(String.Format("Honda has been assigned the number {0}", _
CType(CarManufacturers.Honda, Integer))
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
5/16
Conditional Branching
Branching statements give you control of how your program will flow.
IF & Else Statements
If expression Then
statements
End If
If expression Then
statements
Else
statements
End If
So lets say you want to compare two peoples year of birth:
Dim johnsYear As Integer = 1960
Dim bobsYear As Integer = 1974
If johnsYear > bobsYear Then
Response.Write("John is older!")
Else
Response.Write("Bob is older!")
End If
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
6/16
ElseIf
The keyword ElseIf allows you to perform many If statements, so if the first If statement evaluates to
false, then it will continue on to the next ElseIf. Once you reach the first ElseIf that evaluates to True no
other ElseIf statement will be evaluated.
If johnsYear > bobsYear Then
statements
ElseIf johnsYear > 1950 Then
Statements
..
..
End If
IFF is yet another conditional branching option which reads "If and only If". There are 3 key parts to
using IFF, they are:
# The Boolean expression that you are testing.
# The value that is going to be returned if the Boolean expression returns a value of True.
# The value that is going to be returned if the Boolean expression returns a value of False.
Basically you can write this expression using a combination of If and Else statements, but using the IFF
results in only 1 line of code.
Say you want to assign a integer variable a value of 10 if John is older, or 5 if he is younger than Bob, the
code would look like:
Dim someVariable As Integer
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
7/16
someVariable = CInt(IFF(johnsYear > bobsYear), 10, 5))
Select Case Statement
Usually if you have many IF/ELSE/END If combinations, you can clean up your code and use a more
elegant looking Select Case statement. Basically you are forcing your program to find a match and
execute the resulting code.
Select johnsYear
Case 1920
Response.Write("You old man!")
Case 1950
Response.Write("Mid life crisis are we?")
Case 1970
Response.Write("Just say no to drugs!")
Case 1990
Response.Write("Change my diaper!")
End Select
To make things even cleaner, you could create an Enumeration for the years and use that in your Select
statement.
Looping in VB
VB.NET ships with many interation type statements, including: For Each, For, Do and the GoTo.
For Each Looping Statement
With the for each looping statement you can loop through various items in a collection.
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
8/16
Example: (You have an array of strings)
Dim strCars As String() = {"Honda", "Toyota", "Ford"}
Dim strCar As String
For Each strCar in strCars
Response.Write("
Car: " & strCar")
Next strCar
For Loop
This is a very popular form of looping, one that you will see used very often whilst coding.
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
9/16
Example:
Dim myCounter As Integer = 0
For myCounter To 100
Response.Write("Still Looping!")
Next
Do Looping
The Do loop is basically a loop that continues until a condition becauses True/ False.
There are different variants of this type of loop, such as Do While, Do & Loop Until, Do Until, Do & Loop
While.
Example of a Do While loop:
Dim iMaximumAge As Integer = 35
Dim iMyAge As Integer = 28
Do While iMaximumAge > iMyAge
iMyAge = iMyAge + 1
Loop
The other forms of Do looping is:
Do Until
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
10/16
Loop
Do
Loop While
Do
Loop
Do
Loop Until
While
End While
GoTo Statement
This statement should be used with caution as it often results in very confusing code. The GoTo
statement consists of 2 basic elements:
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
11/16
1) The Label - this consists of the name of the label followed by a colon
2) The 'GoTo' statement that references the label in 1).
Example:
Dim iLoopCount As Integer = 0
Some_label: ' this is the label
iLoopCount = iLoopCount + 1
If iLoopCount < 5 Then
GoTo Some_label
End If
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
12/16
Operators
Operators in Visual Basic .NET include: +, =, >,
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
13/16
Comparison Operator '>, =,
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
14/16
Dim xyz As String = " VB.NET programming"
Dim def As String = abc & xyz ' or you could have used the + operator
Relational Operators: =, etc.
These operators will compare to values and then return either a True or a False (Boolean).
The '' operator stands for Not Equal, so:
Dim a1 As Integer = 10
Dim a2 As Integer = 20
If a1 a2 Then
Response.Write("Not equal is True!")
End If
Exponential Operator '^'
This operator allows you to raise a number to the power of the exponent. So 2^5 will return 25.
Modulus Operator (Mod)
This operator returns the remainder after a division has taken place. An example would be 10 Mode 3
which will return 1.
Logical Operators
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
15/16
A logical operator allows you to test whether a statements conditions are True or False, or if both
conditions are True or if both conditions are False.
# And - both conditions must result to True
# Or - One of the 2 conditions must result to True
# XOr - results to True ONLY if 1 of the 2 conditions is true
# Not - results to True if the conditions result to False.
If you are confused, maybe you should read up on logical operators:
http://homepages.inf.ed.ac.uk/rbf/HIPR2/logic.htm
Strings
A String type is declared as follows:
Dim myString as String:
A string is initialized as follows:
Dim myString as String = "Hello, World!"
OR
myString = "Hello, World!" ' assuming it was already declared earlier
Strings are Immutable, meaning once you assign a value to them they cannot be changed. Whenever
you assign another value to a string or concatenate a value to a string, you are actually creating a new
copy of a string variable and deleting the old copy of the string. (This has potential performance
considerations if you are doing repeated work on a string variable, use the StringBuilder for such tasks as
it is not immutable ie. The StringBuilder is Muttable).
-
8/9/2019 Basically You Can Write This Expression Using a Combination of if and Else Statements, But Using the IFF Results i
16/16
There are many useful methods avaible to you when playing with strings, some of the are: Trim(),
ToUpper(), ToLower(), SubString(), StartsWith(), Compare(), Copy(), Split(), Remove() and Length.
Arrays
OOP, Classes & Objects
1
Build Your Own ASP.NET Website Using C# & VB.NET
Chapter: UnCategorized
Current Lesson:
Visual Basic .NET Syntax Reference Primer [Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson] DateTime Structure