Beruflich Dokumente
Kultur Dokumente
Net
(Classes,Modules,Methods
, Inheritance)
Namespace
• A Namespace is a group of Classes which
are grouped together.
• To prevent name clashes, types are
considered to reside inside of namespaces.
• System itself is a Namespace. It's a top-level
Namespace.
• System.Windows.Forms is another
Namespace you've met.
• A single form is a Class available to Forms:
System.Windows.Forms.Form
• To create your namespace use the key
word namespace.
Namespace MegaBiz.ProductivityTools.WizardMaster
Public Class SomeClass
‘some code
End Class
End Namespace
End Sub
End Class
Properties
• Properties are members that are accessed like fields but
are actually method calls. The idea is that a class
designer may wish to expose some data values but
needs to exert more control over their reading and
writing than is provided by fields.
• You set up a Property by using the following code stub:
Public Property PropertyName( ) As VaraibleType
Get
End Get
Set(ByVal Value As Integer)
End Set
End Property
Employee class with properties
Public Class Employee
Private EmployeeName As String
Private EmployeeID As Integer
Private DateOfBirth As Date
End Sub
End Class
Constructors
• When a class is instantiated, some initialization
often must be performed before the type can be
used.
• To provide such initialization, a class may define
a constructor.
• It is automatically run whenever an object of the
class is instantiated. Constructor declarations
use the same syntax as regular method
declarations, except that in place of a method
name, the constructor declaration uses the
keyword New.
• Public Class SomeClass
Public Sub New( )
' Do any necessary initialization of the
object
here.
End Sub
End Class
• To invoke the constructor, a new object
must be instantiated:
Dim obj As New SomeClass( )
• Constructors can take arguments, if they are
necessary for the initialization of the object:
Public Class SomeClass
Dim m_value As Integer
Public Sub New(ByVal InitialValue As
Integer)
m_value = InitialValue
End Sub
End Class
• When objects of this class are instantiated, a
value must be provided for the constructor's
argument:
Dim obj As New SomeClass(27)
• Constructors can be overloaded, if desired.
Public Class SomeClass
Dim m_value As Integer
Public Sub New( )
m_value = Date.Today.Day ' for example
End Sub
Public Sub New(ByVal InitialValue As Integer)
m_value = InitialValue
End Sub
End Class
• The constructor that is called depends on the arguments that
are provided when the class is instantiated, as shown here:
Dim obj1 As New SomeClass( ) ' calls parameterless
constructor
Dim obj2 As New SomeClass(100) ' calls parameterized
constructor
Private Constructors
• Public Class SomeClass
Private Sub New( )
' Do any necessary initialization of the object
here.
End Sub
• The only way to instantiate this class is through
the GetInstance() method as:
Dim obj As SomeClass =SomeClass.GetInstance( )
Shared Constructors
Public Class SomeClass
Public Shared SomeStaticField As Integer
Shared Sub New( )
SomeStaticField = Date.Today.Day
End Sub
End Class
• The shared constructor is guaranteed to run
sometime before any members of the type are
referenced.
• Shared constructors may not be overloaded,
nor may they have access modifiers
Methods
• Methods are members that contain code that
needs to be executed.
• Methods are of two types:
1.Subroutine(has no return value)
2.Function(has return value)
• Subroutine definitions look like this:
[ method_modifiers ] Sub [ attribute_list ] _
method_name ( [ parameter_list ] ) [
handles_or_implements ]
[ method_body ]
End Sub
• Eg