1 vb net-language fundamentals
TRANSCRIPT
VB.Net(Language
Fundamentals)
A simple VB.net program
Imports System
Public Module HelloModule
Public Sub Main()
Console.WriteLine(“HelloWorld!!!”)
End Sub
End Module
The Main() method
• A program's Main method can appear within a Visual Basic .NET module
• Because Visual Basic .NET modules are
classes wherein everything is shared, the Shared keyword is not used in such a declaration as in the previous example.
Saving,Compiling and Executing the Program
• Save the file with extension “.vb” .(Test.vb)
• Goto command window
• Compile the file :• vbc Test.vb
• After successful compilation .EXE file is created(Test.EXE)
• Execute the EXE file as “Test”
Execution result
Compiling the code
Creating your first VB windows application
Tool box
Form
Properties Box
Solution Explorer
Editing project settings
Writing the first windows application
On the button click write thisPrivate Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Hello World", _
"A first look at vb", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub
Execution of the Program
ByVal and ByRef in VB.net• The word ByVal is short for “By Value”
• This means passing a copy of a variable to your Subroutine in which case changes that are made are made to the copy and the original will not be altered.
• ByRef is short for “By Reference”.
• This means that you are not handing over a copy of the original variable but pointing to the original variable.
Example • Let us consider the example
Dim Number1 As Integer Number1 = 10
Call IncrementVariable(Number1)MsgBox(Number1)-------------------------------------Private Sub IncrementVariable(ByVal Number1 As
Integer) Number1 = Number1 + 1
End Sub
• Try the code with ByRef Number1 as Integer as argument to the function
Fundamental Data Types in VBVB type Range
Boolean True or false
Byte 0 to 255 ( unsigned 8 bit)
Short -32768 to 32767 (signed 16 bit )
Char Any Unicode value (new to vb.net)
U0000 to Uffff (16 bit unicode character)
Date -The range of values is from midnight
on January 1, 0001 (0001-01-01T00:00:00) through 1 second before midnight on December
31, 9999 (9999-12-31T23:59:59).
VB type Range
Long 9223372036854775808 through
9223372036854775807.
Float 1.5*10-45 to 3.4*1038 (32 bit floating point number)
Double 5.0*10-324 to 1.7*10308 (64 bit floating point number)
Decimal 100 to 1028(96-bit signed number)
String Limited by system memory (Represents a set of unicode characters)
Single -3.40282347E38
through 3.40282347E38.(32 bit value) Object Any type can be stored in an object
variable(Base class of all types in .NET)
The System Data TypesVB type System Type Range
Byte System.Byte 0 to 255 ( unsigned 8 bit)
Short System.Int16 -32768 to 32767 (signed 16 bit )
Char System. Char Any Unicode character
U0000 to Uffff (16 bit unicode character)
Date System.DateTime -The range of values is from midnight
on January 1, 0001 (0001-01-01T00:00:00) through 1 second before midnight on December
31, 9999 (9999-12-31T23:59:59).
VB type System Type Range
Long System.Int64 9223372036854775808 through
9223372036854775807. Float System.Single 1.5*10-45 to 3.4*1038 (32 bit floating point
number)
Double System.Double 5.0*10-324 to 1.7*10308 (64 bit floating point number)
Boolean System.Boolean true or false
Decimal System.Decimal 100 to 1028(96-bit signed number)
String System.String Limited by system memory (Represents a set of unicode characters)
Single System.Single -3.40282347E38
through 3.40282347E38.(32 bit value)
Object System.Object Any type can be stored in an object variable(Base class of all types in .NET)
Type Conversion
Declaring and Initializing variables• We declare variables like this: Dim number1 As Integer
Dim number 2 As Integer
• We initialize the variables as: number1 = 3
number2 = 5
• Variable identifiers may be suffixed with type characters that serve to indicate the variable's type. The declaration can be rewritten as
Dim number1% and Dim number2%
List of type charecters
List of literal formats
Introducing Array Types• Array declaration and initialization
– Dim a(4) As Integer;– Dim a as String ={“first” ,”second”, “third”}
– Dim a(,) As Integer{{1,2},{3,4},{5,6}}
• The above declaration produces the following array:
a(0,0)=1 a(0,1)=2a(1,0)=3 a(1,1)=4a(2,0)=5 a(2,1)=6
• For Allocating arrays dynamically we use the “New” keyword
Dim a() As Integer a=New Integer(4){1,2,3,4}
• If the array elements won't be initialized by the allocation, it is still necessary to include the curly brackets:
Dim a( ) As Integer a = New Integer(5) {}
Can you figure out what is the meaning of:
Dim a() As Integer?
Access Modifiers
Operators
+ - / * ++ -- %
== <> < > >= <=TypeOf…IS
Is Like
&& || ! & | ^ ~
Relational Operators
Logical Operators
Arithmetic Operators
Shift Operators
>> <<
= >= <= *= /= &= |= ^= <<= >>= %=
Assignment Operators
Looping Statements
FOR Loop :
For variable = expression To expression [ Step
expression ]statements Next [ variable_list ]
• Eg: Dim answer As Integer
Dim startNumber As Integer
answer = 0
For startNumber = 1 To 4
answer = answer + startNumber
Next startNumber
MsgBox answer
do loopDo While [expression]
statements
Loop• Eg: Dim number as Integer
number = 1Do While number < 5
MsgBox numbernumber = number + 1
Loop
Do…Until loopDo Until [expression]
statements Loop
• Eg: Do Until number < 5
MsgBox numbernumber = number + 1
Loop
For Each For Each variable In expression
statements
Next [ variable ]
• EgDim a( ) As Integer = {1, 2, 3, 4, 5}
Dim b As Integer
For Each b In a
Console.WriteLine(b)
Next
Decision constructsIf/Else statement
If expression Then
statements
End If
also
If expression Then
statements
Else
statements
End If
Condition must evaluate to bool value
Select Case statements
Dim creamcake As StringDim DietState As String creamcake= TextBox1.TextSelect Case creamcake
Case "Eaten"DietState = "Diet Ruined"
Case "Not Eaten"DietState = "Diet Not Ruined"
Case ElseDietState = "Didn't check"
End SelectMsgBox DietState