Académique Documents
Professionnel Documents
Culture Documents
parts:
Namespace declaration
A class or module
One or more procedures
Variables
The Main procedure
Statements & Expressions
Comments
Hello World
Imports System Module Module1
'This program will display Hello World
Sub Main()
Console.WriteLine("Hello World")
Console.ReadKey()
End Sub
End Module
Storage
Allocation
Value Range
Boolean
Depends on
implmnts
True or False
Byte
1 byte
Char
2 bytes
Date
8 bytes
Decimal
16 bytes
0 through +/79,228,162,514,264,337,593,543,950,335
Double
8 bytes
Integer
4 bytes
Long
8 bytes
-9,223,372,036,854,775,808 through
9,223,372,036,854,775,807(signed)
Data Type
Storage
Allocation
Value Range
Object
SByte
1 byte
Short
2 bytes
Single
4 bytes
String
Depends on
implemnts
UInteger
4 bytes
ULong
8 bytes
UserDefined
UShort
2 bytes
Variable Declaration
[ < attributelist> ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [
Static ]] [ Read Only ] Dim [ WithEvents ] variablelist
attributelist is a list of attributes that apply to the variable. Optional.
accessmodifier defines the access levels of the variables, it has values as Public, Protected, Friend, Protected Friend and Private. Optional.
Shared declares a shared variable, which is not associated with any specific
instance of a class or structure, rather available to all the instances of the
class or structure. Optional.
Shadows indicate that the variable re-declares and hides an identically
named element, or set of overloaded elements, in a base class. Optional.
Static indicates that the variable will retain its value, even when the after
termination of the procedure in which it is declared. Optional.
Read-only means the variable can be read, but not written. Optional.
WithEvents specifies that the variable is used to respond to events raised by
the instance assigned to the variable. Optional.
Variablelist provides the list of variables declared.
Variable Declaration
Module variablesNdataypes
Sub Main()
Dim a As Short
Dim b As Integer
Dim c As Double
a = 10
b = 20
c=a+b
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
Console.ReadLine()
End Sub
End Module
Option Strict
Option Strict [On Off]
By default Option Strict is Off
Option Strict
The program is a normal vb.net program and is in default Option
Strict Off .
Because its Option Strict Off we can convert the value of Long to an
Integer.
By adding the below line we can turn strict option on.
Option Strict On
When you write this source code the compiler will shows the
message
"Option Strict On disallows implicit conversions
from 'Long' to 'Integer'"
The compiler generate error because in the program we
put "Option Strict On" and prevent the program from
automatic conversion.
Option Explicit
By default it is declared ON.
Option Compare
The Option Compare statement must appear in a module before
any procedures.
The
Option
Compare
statement
specifies
the
string
comparison method (Binary, Text) for a module.
If a module doesn't include an Option Compare statement, the
default text comparison method is Binary.
Option Compare Binary results in string comparisons based on
a sort order derived from the internal binary representations of the
characters
A<B<E<Z<a<b<e<z<
Option Compare Text results in string comparisons based on a caseinsensitive text sort order determined by your system's locale.
(A=a) < ( =) < (B=b) < (E=e) < (=) < (Z=z) < (=)
Operators
Arithmetic Operators
Operator Description
^
MOD
Comparison Operator
Operator
Description
==
Checks if the values of two operands are equal or not; if yes, then
condition becomes true.
<>
Checks if the values of two operands are equal or not; if values are
not equal, then condition becomes true
>
Checks if the value of left operand is greater than the value of right
operand; if yes, then condition becomes true
<
Checks if the value of left operand is less than the value of right
operand; if yes, then condition becomes true.
>=
<=
Checks if the value of left operand is less than or equal to the value of
right operand; if yes, then condition becomes true.
Logical Operators
Operator
Description
And
Or
Not
Xor
AndAlso
OrElse
IsFalse
IsTrue
Assignment Operators
Operator Description
=
+=
It adds right operand to the left operand and assigns the result to left
operand
-=
It subtracts right operand from the left operand and assigns the result to
left operand
*=
It multiplies right operand with the left operand and assigns the result to
left operand
/=
It divides left operand with the right operand and assigns the result to left
operand (floating point division)
\=
It divides left operand with the right operand and assigns the result to left
operand (Integer division)
^=
It raises the left operand to the power of the right operand and assigns the
result to left operand.
<<=
>>=
&=
Decision Making
If - Then statement
If condition Then
[Statement(s)]
End If
If (a <= 20) Then
c= c+1
End If
If(condition1)Then
'Executes when the condition1 is true
If(condition2)Then
'Executes when the condition2 is true
End If
End If
Loops
Do while
Loops (cntd)
Do
Console.WriteLine("value of a: {0}", a)
a=a+1
Loop While (a < 20)
Loops (cntd)
For loop
Loops (cntd)
For a = 10 To 20 step 2
increments the counter by two
Console.WriteLine("value of a: {0}", a)
Next
Loops (cntd)
For Each element [ As datatype ] In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ] Next [ element ]
Loops (cntd)
Dim anArray() As Integer = {1, 3, 5, 7, 9}
Dim arrayItem As Integer
'displaying the values
For Each arrayItem In anArray
Console.WriteLine(arrayItem)
Next
Console.ReadLine()
Loops (cntd)
While condition
[ statements ]
[ Continue While ] [ statements ]
End While
While a < 20
Console.WriteLine("value of a: {0}", a)
a=a+1
End While
Description
Date
Day
DayOfYear
Hour
Kind
Month
Now
Gets a DateTime object that is set to the current date and time on
this computer, expressed as the local time.
Ticks
Gets the number of ticks that represent the date and time of this
instance.
UtcNow
Gets a DateTime object that is set to the current date and time on
this computer, expressed as the Coordinated Universal Time (UTC).
Year
Type Conversions
Dim dbvalue As Double
Dim intvalue As Integer
Dbvalue =6.4859
intValue=Cint(dbvalue)
converts double to integer
Numvalue=Val(Stringlvariable)
Numvalue=Val(66)
Stringvariable=Str(NumValue)
Stringvarialbe=Str(66)
CBool
CByte
CChar
CDate
CDbl
CInt
Clng
Cobj
CSByte
CShort
CSng
Cstr
Arrays
Dim Data(30) As Integer
8}}
Dynamic Arrays
Use Dim statement to declare an array with empty
Exception Handling
On Error Go to handler
Sub main()
Dim int1=0, int2 = 1 int3 as Integer
On Error Goto Handler
Int3 = int2/int1
System.Console.Writeline(the answer is , int3)
Exit sub
Handler :
System.console.Writeline(Divide by Zero error)
Resume Next
End Sub
Try Catch
Sub main()
Dim int1=0, int2 = 1 int3 as Integer
Try
Int3 = int2/int1
System.Console.Writeline(the answer is , int3)
Catch e As Exception
System.console.writeline(e.ToString)
End Try
End Sub
Throw
If an exception occurs during the evaluation of an
GDI+
GDI stands for Graphic Device Interface. In
System.Drawing.Printing
GDI+ example
Dim g As Graphics = e.Graphics
Dim p As Pen = New Pen(Color.Crimson, 3)
Dim P1 As Point = New Point(10, 10)
Dim P2 As Point = New Point(100, 40)
g.DrawLine(p, P1, P2)
g.DrawLine(New Pen(Color.Crimson, 3), 10,
10, 100, 40)
e.Graphics.DrawLine(New
Pen(Color.Crimson, 3), P1, P2)
Shared Keyword
Using shared keyword makes a member into a class
member, there is no need to create an object to access
that member.
Public Class Maths
Shared Function Add(Byval X As Integer,ByVal Y As
Integer) As Integer
Return X+Y
End Function
End Class
Textbox3.text=Maths.Add(Textbox1.Text,Textbox2.Text)
Constructors
Constructor is special method of a class which is
Constructors(cntd)
Class circle
Private radius as Integer
Private Const PI As Single = 3.14f
Public Sub New (ByVal x As Integer)
radius=x
End Sub
Public Sub area()
Dim res As Single = PI*radius*radius
Console.writeline(Area of a circle is &res)
End Sub
End Class
Module Program
Sub Main()
Dim obj As New circle(5)
Obj.area()
Console.Read()
End Sub
End Module
Abstract Class
Inheritance is a way to extend the functionalities of a class in
inherited first.
It can contain any number of abstract methods.
It can also contain non abstract methods.
It is declared using the MustInherit keyword.
Interface
It is not possible for a class to inherit simultaneously
Characteristic of interface
It is declare in the same way as we declare classes.