|
| Classes
VB.NET classes are the same in principle as they were in VB6. However, the way you create properties
is different. Where you defined properties in VB like the example below.
| Visual Basic 6.0 |
dim strComment as string
dim strName as string
Private Sub Class_Initialize()
strName="PropertyClass"
End Sub
Public Property Get Comment() as string
Comment = strComment
End Property
Public Property Get Name() as string
Name = strName
End Property
Public Property Let Name(strNew as string)
strName = strNew
End Property
|
GET/LET and SET operations were separate entities, now for VB.NET they're not. A LET property has
been removed so that for the above, the code would look like :-
| Visual Basic.NET |
dim strComment as string
dim strName as string = "PropertyClass"
dim strTime as string
Public ReadOnly Property Comment As string
Get
Return strComment
End Get
End Property
Public Property Name as string
Get
Return strName
End Get
Set (ByVal strNew as string)
strTime = strNew
End Set
End Property
|
As you can see from above, in the Comment Property, there is a ReadOnly word which
tells the compiler not to look for a SET section of the property. For a property which can
only have a property set and not got, use WriteOnly instead. Another new feature of VB.NET is inheritence, below is a code example :-
| Visual Basic.NET |
Imports Microsoft.VisualBasic
Public Class clsItem
Private mlngID As long
Private mdtBirthdate As date
Public Overridable Property UID() As long
Get
Return mlngID
End Get
Set(ByVal Value As long)
mlngID = Value
End Set
End Property
Public Overridable Property BirthDate() As Date
Get
Return mdtBirthDate
End Get
Set(ByVal Value As Date)
mdtBirthDate = Value
End Set
End Property
End Class
Public Enum eGender
Female
Male
End Enum
Public Class clsPerson:Inherits clsItem
Private meGender As eGender
Private mstrName as string
Private mdtBirthDate as date
Public Overloads Property UID() As String
Get
Return mstrName
End Get
Set(ByVal Value As String)
mstrName = Value
End Set
End Property
Public Overrides Property BirthDate() As Date
Get
Return mdtBirthDate
End Get
Set(ByVal Value As Date)
mdtBirthDate = Value
End Set
End Property
Public Property Gender() As eGender
Get
Return meGender
End Get
Set(ByVal Value As eGender)
meGender = Value
End Set
End Property
End Class
Module One
Sub Main()
Dim objPerson as new clsPerson
objPerson.UID = "Nigel Whitworth"
System.console.write(objPerson.UID)
End Sub
End Module
|
As you can see from the above example, there is Overrideable, Overloads and Overrides. They are
all standard OO techniques. You can only override or overload a property from the base class that
is overrideable. Overrides means this replaces the code in the base class. Overloads enables you
have multiple functions that have the same name. The compiler runs the function that matches the
parameters that is passed into the function.
Make a comment*Comments are the views of individuals, they may or may not be correct. All comments are reviewed and accepted or rejected. If you give an email address, you will be sent an email when someone makes a comment on this page.* | Only name and Code is compulsory. |
|