You are on page 1of 4

Syntax of VB.

NET Syntax of Visual C#


► VB.NET is not case sensitive ► Visual C#.NET is a case sensitive.
► Every statement end with semicolon
Variables called statement terminator.
☺ Declaration of a Variable Variables
 Syntax: ☺ Declaration of a Variable
DIM (Variable Name) AS Datatype
*DIM is keyword; however we can use PUBLIC and
 Syntax:
PRIVATE keyword also. DataType variable_name;

☺ Initialization of a Variable ☺ Initialization of a Variable


 Design time Initialization  Design time Initialization
DIM variablename AS datatype = value Datatype variable_name = value;
For example For example
DIM abc AS string = “hello” string country = “Pakistan”;
 Run Time Initialization  Run Time Initialization
DataType variable_name;
DIM variable_name AS datatype Vaiable_name=value;
Variablename = value/location For example
For example Int mynumber;
DIM abc As String mynumber=5;
Abc = textbox.text

Control Structures Control Structures


☺ IF-THEN-ELSE ☺ IF-THEN-ELSE
 Syntax  Syntax
IF (condition)
IF condition THEN {
Statements if condition true Statements if condition true;
ELSE }
ELSE
Statements if condition false {
END IF Statements if condition false;
}
 For Example  For Example
Dim a as integer=2 Int =2 ;
Dim b as integer=3 int = 3;
IF A>B THEN IF (A>B )
Messagebox.show (“A is largest”) { Messagebox.show (“A is largest”); }
ELSE ELSE
Messagebox.show (“B is largest”) { Messagebox.show (“B is largest”); }
END IF
☺ IF-ELSE IF
Prepared by: MUHAMMAD IMRAN Page 1
Copyright © 1994-2010
 Syntax  Syntax
IF (condition) THEN IF (condition)
Do this {
ELSEIF (condition) THEN Do this;
}
Do this
ELSEIF (condition)
ELSEIF (condition) THEN {
Do this Do this;
END IF }
 For Example ELSE
{Do this;
Dim a as integer=2 }
Dim b as integer=3  For Example
IF (A>B) THEN Int =2 ;
Messagebox.show (“A is largest”) int = 3;
ELSEIF (B>A) THEN IF (A>B)
Messagebox.show (“B is largest”) { Messagebox.show (“A is largest”); }
ELSEIF (A=B) THEN ELSEIF (B>A)
Messagebox.show (“A equals to B”) { Messagebox.show (“B is largest”); }
ELSEIF (A=B)
END IF
{ Messagebox.show (“A equals to B”); }
☺ Select Case ☺ Switch
 Syntax
 Syntax Switch (variable/Expression)
SELECT CASE (variable/Expression) { Case I:
Case I Do something;
Do something Break;
Case II Case II:
Do something Do something;
Break;
END SELECT
Default:
 For Example Break; }
Dim a as integer=2  For Example
Dim b as integer=3 Int =2 ;
Dim abc AS string int = 3;
Abc =textbox.text string abc = textbox.text;
Select case abc Switch (abc)
Case “+” { Case “+”:
Messagebox.show (“Add Result =” & a+b);
Messagebox.show (“Add Result =” & a+b)
Break;
Case “-” Case “-”:
Messagebox.show (“subtract Result =” & a-b) Messagebox.show (“subtract Result =” & a-b);
Case “*” Break;
Messagebox.show (“Multiply Result =” & a*b) Default:
End Select Messagebox.show (“Press +/-”); }

☺ IF-ELSE IF

Prepared by: MUHAMMAD IMRAN Page 2


Copyright © 1994-2010
LOOPS LOOPS
☺ FOR-NEXT LOOP ☺ FOR-NEXT LOOP
 Syntax  Syntax
FOR variable=I. value TO E. value Step value FOR
(initialization;condition;increment/decrement)
Statements
{
Statements Statements
Next variable }
 I. value = Initial value For Example
 E. value = Ending value For (int I =2; I<10 ; i++)
 For Example {
Messagebox.show (I.tostring( ));
For X=1 TO 20 STEP 2
}
Messagebox.show (x)
☺ WHILE (Pre-Test)
Next X
 Syntax
☺ DO-WHILE (Pre-Test) WHILE (condition)
 Syntax {
DO WHILE (condition) Statements
Increment/decrement
Statements
}
Statements
 For Example
Increment/decrement int=1;
LOOP WHILE (check<=4)
 For Example {
Dim check AS integer=1 Messagebox.show (check);
Check +=1;
DO WHILE (check<=4)
}
Messagebox.show (check)
Check +=1
☺ DO-WHILE (Post-Test)
LOOP
 Syntax
☺ DO-WHILE (Post-Test) DO
 Syntax {
DO Statements
Increment/decrement
Statements
}
Statements WHILE (condition);
Increment/decrement  For Example
LOOP WHILE (condition) int=1;
 For Example DO
Dim check AS integer=1 {
DO Messagebox.show (check);
Messagebox.show (check) Check +=1;
Check +=1 }
LOOP WHILE (check<=4) WHILE (check<=4);
ARRAYS
Prepared by: MUHAMMAD IMRAN Page 3
Copyright © 1994-2010
☺ One-Dimension Arrays ☺ One-Dimension Arrays
 Declaration of an Array  Declaration of an Array
Dim ArrayName(UpperSubscript) As Datatype Datatype[ ] array_name;

☺ Intialization of an Array  Instantiate an Array


 Design time Initialization Datatype[ ] array_name;
Dim abc ( ) as integer= {1,2,3,4,5} Array_name = new datatype[100];

Dim country as string ( ) = {“Pakistan”,”Dubai”}


☺ Initialization of an Array
 Run Time Initialization  Design time Initialization
Dim abc (5 ) as integer
For x=1 to 5 array_name[ position_in_array ] = array_value;
Abc(x) = X * X Int[ ] integers = new int[4] {1,2,3,4,5};
Messagebox.show(abc(x))
Integers[4] = 125;
Next X
 Run Time Initialization
Int[ ] integers = new int [5];
For (int x=1,x<=4;x++)
{
integers(x) = X * X;
Messagebox.show (integers(x).Todtring() );
}

ARRAYS

Prepared by: MUHAMMAD IMRAN Page 4


Copyright © 1994-2010

You might also like