Part I
Learn VB .NET fast!
So what's the difference between vb 6 and vb .net?
you may have asked yourself that but when you find the answer you will be
almost surprised.
The first difference and the most important one is that vb
.NET is a complete Object Oriented Programming language with all the options in
an OOP language like classes , Implementation , Inheritance etc.
Here is a brief description of each of these new features:
(Notice that I removed the Public statement from the
beginning of the class declarations because it is not almost needed when
working with only one module)
Class
A class is a set of properties , and methods in one
piece of code that can be used multiple times. a class
itself is not an object but objects can be created from classes.
The following code demonstrates a simple class:
Class SimpleClass
Public Sub
SimpleMethod()
System.Console.Write("Simple Class")
End Sub
End
Class
The above code shows the declaration of the SimpleClass(note that
this class have no use unless you make an Instance of it)
The following code shows how to use the SimpleClass class:
Class Prog1
Public Shared Sub Main()
Dim obj as New SimpleClass()
obj.SimpleMethod()
End Sub
End Class
Any program in vb .NET must have at least one
class (or module) with a 'Main' method which is where the program starts. This
sub must be shared (explained later)
in the above example the program's class is called "Prog1" and it
has a main method and an instance of the SimpleClass class is created using the
'Dim' statement , Dim statement is used to declare Variables and Objects. 'New'
statement makes a new instance and gives it the memory space it needs ,
remember that if you don't use the 'New'
// statement you will only get a
reference no an object unless you set it to an object again using the 'New'
statement.
To separate a method or property from its owner object you must
use dot ('.') like obj.SimpleMethod()
Class members
Class members can be Public , Protected and Private.
Members that are public are visible to objects derived from the
class, classes that inherit this class and the class itself.
Members that are private are only visible to the class itself.
Member Type/What can access it |
The class itself |
Derived Classes |
Objects derived from the class |
Public |
Yes |
Yes |
Yes |
Protected |
Yes |
Yes |
No |
Private |
Yes |
No |
No |
Members that are protected can be used in the class itself and
classes that inherit the class.
The following example shows how to use different member kinds
Class SimpleClass2
Private
MemberVar1 as Long
Protected MemberVal2 as
Long
Public MemberVal3 as
Long
End Class
Class Prog2
Public Shared Sub Main()
Dim obj as New
Simpleclass2()
'obj.MemberVal1 =
1 'wrong because
it's a private member
'obj.MemberVal2 =
2 'wrong because
it's a protected member
obj.MemberVal3 =
3 'correct because
it's a public member
End Sub
End Class
Constructors
Sometimes you want to give some members of a class some
initialization value when the object is created . This can be done by using the
'New' as sub.
The following code shows how to use them:
Class SimpleClass3
Public Var as
Long
Public Sub New()
Var = 10 'Initialization value
End
Sub
End Class
Class Prog4
Public Shared Sub Main()
Dim obj as New
Simpleclass3()
System.Console.Write(obj.Var)
End Sub
End Class
When the object is created , the value of Var is set to 10.
You also send parameters to the constructor like this:
Class SimpleClass4
Public Var
as Long
Public Sub New(InitValue
as Long)
Var = InitValue 'Initialization value
End
Sub
End Class
Class Prog5
Public Shared Sub Main()
Dim obj as New
Simpleclass4(1000) 'The initialization value is 1000
System.Console.Write(obj.Var)
End Sub
End Class
Shared Methods
Sometimes you want to write a class that has methods which are not
related to a specific object e.g. a class that has math methods. So you want to
use the methods without creating an object first. In this case you can declare
it using the 'Shared' statement the following code is an example of shared
methods:
NOTE: shared methods and properties can not access methods,
properties or variables that are not shared.
Class SimpleClass5
Public Shared Function Multiply (ByVal Num1 as Long , ByVal Num2 as Long) as
Long
Return Num1*Num2
End Sub
End Class
Class Prog6
Public Shared Sub Main()
System.Console.Write(SimpleClass5.Multiply(10,10))
End Sub
End Class
Part II
Inheritance
This subject is almost the most important in OOP, it makes writing
codes a lot faster because you don't have to write code for each of the classes
and instead you write a general class and other classes will inherit it. for
example in a game you write a general character class then you can write more
specific classes that inherit this class for example enemy class and friend
class and again more detailed classes which inherit these items.
And the great thing is that inheritance is available in VB .NET.
the following example shows how to use it:
Class A '
parent class
Public Sub A()
System.Console.Write("A")
End Sub
End Class
Class B '
fist child class
Inherits A ' this line tells VB that this class inherits A
Public Sub B()
System.Console.Write("B")
End Sub
End Class
Class C 'second
child class
Inherits B ' this line tells VB that this class inherits A
Public Sub C()
System.Console.Write("C")
End Sub
End Class
Class Prog6
Public Shared Sub Main()
Dim objC as New C() ' always remember to use parentheses when using the
New statement
objC.A() 'because C inherits B and B inherits A so it has A() too
objC.B() 'inherits B() directly from B
objC.C() 'classes own sub
End Sub
End Class
Abstract classes (MustInherit)
Some classes are not used to create object directly from them but
are used to write classes that inherit them. Following code shows this matter:
MustInherit Class A
' parent class
Public Sub A()
System.Console.Write("A")
End Sub
End Class
Class B '
fist child class
Inherits A ' this line tells VB that this class inherits A
Public Sub B()
System.Console.Write("B")
End Sub
End Class
Class Prog7
Public Shared Sub Main()
'Dim objA as New A() 'this is wrong because
you can not create an instance from a mustinherit class
Dim objB as New B() ' always remember to use parentheses when using the New
statement
objB.A()
objB.B()
End Sub
End Class
Overriding
When writing a class that inherits another class you may want to
change what a sub or function in the base class does in this case you can
use overriding:
Class A '
parent class
Public Overridable Sub A()
System.Console.Write("A.A")
End Sub
End Class
Class B '
fist child class
Inherits A ' this line tells VB that this class inherits A
Public Overrides Sub A()
System.Console.Write("B.A")
End Sub
End Class
Class Prog8
Public Shared Sub Main()
Dim objB as New B()
objB.A() ' the output will be "B.A" not "A.A"
End Sub
End Class
Events
If you have used VB 6 or any other windows programming
language then you must be already familiar with events.
An event is a piece of code (a procedure) which executes
when something especial happens such as the users clicks a button , or
text of a textbox changes.
classes can have events. Following code shows how to use
events:
Class A
Public Event OnInitialize ' declares the event as public
Public Sub New()
RaiseEvent OnInitialize() ' causes the event to be executed
End Sub
Public Overridable Sub A()
System.Console.Write("A.A")
End Sub
End Class
Class Prog9
Private WithEvents objA as New A() ' notice 'WithEvents' it tells vb that
this class has events
Public Shared Sub Main()
Dim objB as New B()
objB.A() ' the output will be "B.A" not "A.A"
End Sub
Private sub objA_OnInitialize() Handles objA.OnInitialize '
"Handles" tells vb that this sub is the event handler for the "objA.onInitialize"
event
System.Console.WriteLine("objA initialized")
End Sub
End Class
This time it has the sample files!
More coming soon!
|