You are on page 1of 43

.





...





2

: (:

3

2 ....................................................................................................

7 ............................................................................ :

7 .......................................................................... :

8 .............................................................................................

10 ....................................................................................... :

11 ............................................................... :variables

12 .............................................................. :Constants

12 .......................................................................... :Comments

13 ...................................................................... :Data type

14 ............................................................................. :operators

16 .............................................................. :Flow control

16 ..................................................................................... if...else
17 ............................................................................... :
18 ............................................................................. :
18 ......................................................................... :Loops

4
18 ........................................................................................ : for
19 ......................................................................................... : do
20 ...................................................................................... : while
20 .................................................................................. : for each
21 ....................................................................................... :goto
22 .............................................................................. :Methods

22 ........................................................................................ :
23 ..................................................................................... :
23 ........................................................................................ :
25 ................................................................................ :arrays

25 ........................................................................... :structures

27 ...................................................................... :Enumerations

28 ............................................ :Handling Exceptions

30 ................................................ :Classes and Objects

Me 31 .......................................................................................... :this

32 ........................................................................... :Inheritance

34 ........................................................................... :Interfaces

5
36 ....................................................................... :Constructors

37 ......................................................................... :Properties

39 ....................................................................................... :

39 .............................................................. :namespaces

40 ................................................................................ :

41 ........................................................ : static members

43 ................................................................................................. :

6
:

:

.

2002 Framework 1.0



.

:
Basic
Beginners All-purpose Symbolic Code
.1963

7
1990 Visual Basic 1.0
6

.


console
.asp.net

(:

}{

:
(:

8



6
VBA
.

Oriented Object Programming



Procedural
Modules .


Operator Overloading
Input Box
.

:
.
9
:

.

Dim number As Integer


Dim Number As Integer 'Error

;int number
)int Number; //No problem :


.

Dim 1number as Integer 'Error


Dim $number as Integer 'Error
Dim class as Integer 'Error

int 1number; //Error


int $number; //Error
;int class
; //Error

10
:variables
Runtime
Data Type
.

Dim number As Integer


Dim number2 As Integer = 15
Dim name As String
Dim "name2 As String = "Ahmed

;int number
;int number2 = 15
;string name
;"string name2 = "Ahmed

11
:Constants

.

Const pi As Double = 3.14


"Const state As String = "UnKnown

;const double pi=3.14


;"const string state = "UnKnown

:Comments
Compiler
.

'This is a single - line comment

'This is
'a multi - line
'Comment

12
//This is a single - line comment

/*This is
a multi - line
Comment*/

:Data type

. boolean integer string

#
int Integer
long Long
short Short
byte Byte
float Single
double Double
decimal Decimal
DateTime Date
string String
char Char
bool Boolean
object Object

13
:operators

.

#

+ +
- -
* *
/ /
/ \
% Mod
^

;int number Dim number As Integer

;number = 10 number = 10

;int number Dim number As Integer

;number += 10 number += 10

;int number Dim number As Integer

;number -= 10 number -= 10

;int number Dim number As Integer

;number *= 10 number *= 10
14
;int number Dim number As Integer

;number /= 10 number /= 10

;int number Dim number As Integer

;number /= 10 number \= 10

;"string name="Ahmed "Dim name As String="Ahmed

"name +=" is a teacher"; name &=" is a teacher


> >
=> =>
< <
=< =<
== =
=! ><

&& and )(
|| Or )(

Number++
Number--

15
:Flow control

.

if...else

If job = "Doctor" Then


state = True
ElseIf job = "Nurse" Then
state = True
Else
state = False
End If

if (job == "Doctor")
{
state = true;
}
else if (job == "Nurse")
{
state = true;
}
else
{
state = false;
}
}

16
:

Select Case op
Case "+"
result = 5 + 5
Case "-"
result = 5 - 5
Case "*"
result = 5 * 5
Case "/"
result = 5 / 5
Case Else
result = 0
End Select

switch (op)
{
case '+':
'+'
result = 5 + 5;
break
break;
case '-':
'
result = 5 - 5;
break
break;
case '*':
'*'
result = 5 * 5;
break
break;
case '/':
'/'
result = 5 / 5;
break
break;
default
default:
result = 0;
break
break;
}

17
:

Dim access_state As String = IIf(password = "pass123", "correct",


"incorrect")

string access_state=password==
=password=="pass123" ? " correct": "incorrect";
"incorrect

:Loops
.

: for

For number As Integer = 0 To 9


Console.WriteLine(
.WriteLine("Number : "& number)
Next

18
for (int number = 0; number < 10; number++)
{
Console.WriteLine(
.WriteLine("Number : "+ number);
}

: do

Dim number As Integer = 0


Do Until number > 5
Console.WriteLine(
.WriteLine("Line:{0}", number)
number += 1
Loop

int number = 0;
do
{
Console
Console.WriteLine("Line:{0}", number);
number++;
}
while (number <= 5);

19
: while

Dim number As Integer = 0


While number < 9
Console.WriteLine(
.WriteLine("Line :{0}", number)
number += 1
End While

int number = 0;
while (number < 9)
{
Console
Console.WriteLine("Line :{0}",
, number);
number++;
}

: for each

Dim name As String = "Ahmed"


For Each letter As Char In name
Console.WriteLine(letter)
.WriteLine(letter)
Next

20
string name = "Ahmed";
foreach (char
char letter in name)
{
Console
Console.WriteLine(letter);
}

:goto

Dim founder As String


ask: Console.Write(
.Write("who
"who is the founder of microsoft?")
microsoft?"
founder = Console.ReadLine()
Console
If founder = "bill gates" Then
Console.Write(
.Write("You're right")
Else
Console.WriteLine(
.WriteLine("False !!")
GoTo ask
End If

string founder;
ask: Console.Write(
.Write("who
"who is the founder of microsoft?");
microsoft?"
founder = Console.ReadLine();
if (founder == "bill gates")
{
Console
Console.Write("You're right");
}
else
{
Console
Console.WriteLine("False !!");
goto ask;
}

21
:Methods

.

'Create Procedure
Public Sub myProc()
'Do Something
End Sub

'Create Function
Public Function myFunction() As String
Return "Something"
End Function

//Create Procedure
public void myProc()
{
//Do Something
}

//Create Function
public string myFunction()
{
return "Something";
"Something"
}

22
:

myProc()

Dim value As String = myFunction()

myProc();

string value = myFunction();

'passing parameters by value


Public Function myFunction(ByVal
myFunction( a As Integer, ByVal b As
Integer) As Integer

Return a + b

End Function

'Calling the method


Dim sum As Integer = myFunction(10, 35)

//passing parameters by value


public static int myFunction(int a, int b)
{
return a+b;

23
}

//Calling the method


int sum = myFunction(10,35);

'passing parameters by reference


Public Function myFunction(ByRef
myFunction( a As Integer, ByRef b As
Integer) As Integer

Return a + b

End Function

'Calling the method


Dim x As Integer = 3, y As Integer = 9
Dim sum As Integer = myFunction(x, y)

//Passing by reference
public static int myFunction(ref int a, ref int b)
{
return a+b;
}

//Calling the method


int x = 3, y = 9;
int sum = myFunction(ref
myFunction( x,ref y);

24
:arrays
.

'Declaring and Poulating an array


Dim colors() As String = New String() {"red", "green",
}""green" "blue
}Dim numbers() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9

//Declaring and Poulating an array


][string
[] colors = new string[] { "red", "green",
;} ", "blue
][int
;} [] numbers = { 1, 2, 3 , 4 , 5 , 6 , 7 , 8 , 9

:structures

) (...
.

25
'Creating the structure
Structure student
Public name As String
Public age As Integer
Public address As String

Public Function ShowStudent() As String


Return "Student name:" & name _
& "Student age: " & age _
& "Student address: " & address
End Function
End Structure

'Creating a new instance of the student structure


Dim student1 As New student
'initialize fields
student1.name = "Khalid"
student1.age = "24"
student1.address = "Morocco"
'access to its methods
student1.ShowStudent()

//creating the student structure


struct student
{
//Fields
public string name;
public int age;
public string address;

//Methods

26
public string ShowStudent()
{
return "Student name: " + name
+ " Student age: " + age
+ " Student address: " + address;
}
}

//Creating a new instance of the student structure


student student1 = new student();
//initialize fields
student1.name = "Khalid";
student1.age = 24;
student1.address = "Morocco";
//access to its methods
student1.ShowStudent();

:Enumerations


.

'Creating the enumeration


Enum WeekDays
Monday
Tuesday
Wednesday
Thursday

27
Friday
Saturday
Sunday
End Enum

//Creating the enumeration


enum WeekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

:Handling Exceptions
Runtime

...


.try...catch

Dim age As Integer

28
Console.WriteLine(
.WriteLine("Enter your age:")
Try
age = Console.ReadLine
Console
Catch ex As Exception
Console.WriteLine(Err.Description)
.WriteLine(Err.Description)
Finally
Console.WriteLine(
.WriteLine("press
"press any key to leave...")
leave..."
End Try
Console.ReadKey()
.ReadKey()

int age;
Console.Write(
.Write("Enter your age:");
try
{
age = int.Parse(Console.ReadLine());
}
catch (Exception
Exception Err)
{
Console
Console.WriteLine(Err.Message);
}
finally
{
Console
Console.WriteLine("press
"press any key to leave...");
leave..."
}

29
:Classes and Objects
structure

.class

'Creating a class
Public Class Car
'Class members
End Class

'Creating new instance named 'object1' of Car class


Dim object1 As New Car

//Creating a class
public class Car
{
//Class members
}

//Creating new instance named 'object1' of Car class


Car object1 = new Car();

30
:this Me
Me
) this
.

Public Class Employee


Private name As String

Public Function Work() As String


Return "I am a developer"
End Function

Public Sub initialize(Name As String)


Me.name
.name = Name
Me.Work()
End Sub

End Class

public class Employee


{
private string name;

public string Work()


{
return "I am a developer";
developer"
}

public void initialize(string


initialize( Name)
31
{
this.name
.name = Name;
this.Work();
}

:Inheritance
Main class
.Derived class

(... )Animal
.( Animal )

'Main class
Public Class Animal
Private name As String

Public Function getName() As String


Return name
End Function
End Class

'Derived Classes
Public Class Lion
Inherits Animal
End Class

32
Public Class Tiger
Inherits Animal
End Class

Public Class Horse


Inherits Animal
End Class

//Main class
public class Animal
{
private string name;

public string getName()


{
return name;
}
}

//Derived Classes
public class Lion:Animal
Animal
{
}

public class Tiger : Animal


{
}

public class Horse : Animal


{
}

33
:Interfaces

interfaces


.
.implementation

:interface

'Defining ILocation interface


Public Interface ILocation
Sub setLocation(ByVal
ByVal x As Integer, ByVal y As Integer)
Integer
Function GetLocation_X() As Integer
Function GetLocation_Y() As Integer
End Interface

'Implementing ILocation interface


Public Class myForm
Implements ILocation
Private x, y As Integer

Sub setLocation(ByVal
ByVal X As Integer, ByVal Y As Integer)
Integer
Implements ILocation.setLocation
.setLocation
Me.x = X
Me.y = Y

34
End Sub

Function GetLocation_X() As Integer Implements


ILocation.GetLocation_X
.GetLocation_X
Return x
End Function

Function GetLocation_Y() As Integer Implements


ILocation.GetLocation_Y
.GetLocation_Y
Return y
End Function
End Class

//Defining ILocation interface


public interface ILocation
{
void setLocation(int
int x, int y);
int GetLocation_X();
int GetLocation_Y();
}

//Implementing ILocation interface


public class myForm:ILocation
ILocation
{
private int x, y;
void ILocation.setLocation(
.setLocation(int X, int Y)
{
this.x = X;
this.y = Y;
}

int ILocation.GetLocation_X()
.GetLocation_X()
{
return x;
}

35
int ILocation.GetLocation_Y()
.GetLocation_Y()
{
return y;
}
}

:Constructors

.

Public Class Employee


Private name As String
Private age As Integer

'Constructor Without Parameters


Sub New()
Me.New("name
"name not found",
found" 0)
End Sub

'Constructor With Parameters


Sub New(Name As String,
String Age As Integer)
Me.name
.name = Name
Me.age = Age
End Sub

End Class

36
public class Employee
{
private string name;
private int age;

//Constructor Without Parameters


public Employee()
: this("name
"name not found",
found" 0) { }

//Constructor With Parameters


public Employee(string
string Name, int Age)
{
this.name
.name = Name;
this.age
.age = Age;
}

:Properties

.public

Getter
.Setter

37
Public Class Employee
Private name As String
Private age As Integer

Public Property name_property()


Get
Return Me.name
.name
End Get
Set(value)
Me.name
.name = value
End Set
End Property

Public Property age_property()


Get
Return Me.age
.age
End Get
Set(value)
Me.age
.age = value
End Set
End Property

End Class

public class Employee


{
private string name;
private int age;

public string name_property


{
get { return name; }
set { name = value;
value }
}

38
public int age_property
{
get { return age; }
set { age = value;
value }
}
}

:
. " "

Dim emp As New Employee


emp = Nothing

Employee emp=new
new Employee();
emp=null;

:namespaces
Imports
. using

Imports System
Imports System.Collections.Generic
Imports System.Linq

39
Imports System.Text
Imports System.Threading.Tasks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

:
:

MsgBox("" , MsgBoxStyle.Information,
MsgBoxStyle ")"

MessageBox.Show("" ,
"", MessageBoxButtons.OK,
.OK,
MessageBoxIcon.Information);
.Information);

40
: static members

.

Public Class Employee


'static field
Public Shared name As String

'static method
Public Shared Function getName()
Return name
End Function

End Class

Sub Main()
'use static members
Employee.name
.name = "Khalid"
Dim myName As String = Employee.getName
End Sub

public class Employee


{
//static field
public static string name;

41
//static method
public static string getName()
{
return name;
}

static void Main(string[] args)


{
//use static members
Employee.name = "Khalid";
string myName = Employee.getName();
}

42
:



.

:
http://www.mobarmijoun.com

:
Khalid_ESSAADANI@Hotmail.fr

ESSAADANIKHALID@Gmail.com

:
www.facebook.com/ESSAADANI.Khalid

www.facebook.com/Khotwa.Amam

43

You might also like