Sie sind auf Seite 1von 39

VB.

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

• You can also set the root namespace in


visual studio IDE.
Classes
• A class is one form of data type
• In object-oriented design, classes are intended to
represent the definition of real-world objects, such as
customer, order, product, etc.
• A class declaration defines the set of members—
fields, properties, methods, and events—that each
object of that class possesses.
• Together, these members define an object's state, as
well as its functionality. An object is also referred to as
an instance of a class.
• Creating an object of a certain class is called
instantiating an object of the class.
Class definition
Public Class Employee
Public EmployeeName As String
Public EmployeeID As Integer
Public DateOfBirth As Date

Public Function name() As String


Return EmployeeName
End Function

Public Function id() As Integer


Return EmployeeID
End Function

Public Function dob() As Date


Return DateOfBirth
End Function
Using a class
Public Class Form1
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
Dim em As Employee
em = New Employee
em.EmployeeName = TextBox1.Text
em.EmployeeID = TextBox2.Text
em.DateOfBirth = TextBox3.Text

MsgBox("Employee Name----" & em.name() & "---Employee ID----"


& em.id() & "---DOB----" & em.dob)

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

Public Property Name() As String


Get
Name = EmployeeName
'Return EmployeeName
End Get
Set(ByVal value As String)
EmployeeName = value
End Set
End Property
Public Property ID() As Integer
Get
ID = EmployeeID
'Return EmployeeID
End Get
Set(ByVal value As Integer)
EmployeeID = value
End Set
End Property

Public Property DOB() As Date


Get
DOB = DateOfBirth
'Return DateOfBirth
End Get
Set(ByVal value As Date)
DateOfBirth = value
End Set
End Property
End Class
Using property
Public Class Form1
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim em As Employee
em = New Employee
em.Name = TextBox1.Text
em.ID = Int32.Parse(TextBox2.Text)
em.DOB = TextBox3.Text

MsgBox("Employee Name----" & em.Name() & "---Employee ID----" &


em.ID() & "---DOB----" & em.DOB())

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

Private Sub Button1_Click(ByVal sender


As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
End Sub
• Function definitions look like this:
[ method_modifiers ] Function [
attribute_list ] _
method_name ( [ parameter_list ] ) [ As
type_name ] _
[ handles_or_implements ]
[ method_body ]
End Function
• Eg
Public Shared Function Avg(ParamArray ByVal
Numbers( ) As Integer) As Double
Dim sum As Integer = 0
Dim count As Integer = 0
Dim n As Integer
For Each n In Numbers
sum += n
count += 1
Next
Return sum / count
End Function
Calling a method
• The Call statement invokes a subroutine or
function.
Call SomeMethod( )
• When the invoked subroutine or function
finishes, execution continues with the
statement following the Call statement.
• The Call statement is redundant because
subroutines and functions can be invoked
simply by naming them:
SomeMethod( )
Standard Modules
• Standard module is a type declaration.
• It is introduced with Module statement.
• Standard Modules are good place to put global variables
and procedures that are not logically associated with any
class.
• Standard module definitions are similar to class definitions,
with the following differences:
 Standard modules members are implicitly shared
 Standard Modules cannot be inherited
 The members in a standard module can be referenced without
the standard module name.
Add Module to a project
Example
Use
• The point about creating a Module to
house all your Subs and Functions is that
they are in a separate file.
• If the Subs and Functions were in the
same code for the Form, you would have
to import the whole Form before you could
use the very useful Subs and Functions
you created.
Inheritance
Public Class Person
Private pName As String
Private pDob As Date
`Setters and Getters code
End Class
Creating sub class
Public Class Employee
Inherits Person

Private eHireDate As Date


Private eSalary As Double

`code for setters and getters


End Class
The Application
Some Important keywords
• The MustInherit modifier if used with class
declaration indicates that this class can't be
instantiated—it can only be used as a base class
in a derivation. In object- oriented design
terminology, such a class is known as an
abstract class.
Public MustInheritable Class SomeClass
' ...
End Class

• Using the MustOverride modifier on a method


declaration in a class forces derived classes to
provide an implementation for this method. A
method marked with MustInherit has no body
Public MustOverride Sub Draw( )
• It is possible to define a class from which it
is not possible to inherit. This is done with
the NotInheritable keyword in the class
declaration, as shown here:
Public NotInheritable Class SomeClass
' ...
End Class

• Me keyword provides a reference to the


current object reference.
Me.name
• Any code within a subclass can call any
method or variable on the base class by
using the MyBase keyword.
MyBase.name
MyBase.New()
Overloading methods
• Methods with the same name but different signature are
called overloaded methods.
• Method overloading is used for methods that do
semantically the same thing.
• Methods may differ in terms of
– Number of parameters
– Types of parameter
– Order of parameter
• When the method is called, compiler determines the best
match based on the number, type and the order of the
arguments of the method call statement.
• Note that if two methods are identical except for their return
types or access specifiers then the methods are not
overloaded.
Overloads Function FindCust(ByVal psName As String) As
String
‘ search name field for %psName%
End Function

Overloads Function FindCust(ByVal piCustNo As Integer) As


String
‘ search CustID field
End Function
• Your calls to it could look like this:
Dim x As String
x = FindCust(“Hello”)
x = FindCust(1)
Overriding Methods
• When you inherit a base class, the
properties and methods cannot be
overridden, by default.
Public MustInherit Class Transportation
MustOverride Function Move() As Boolean
End Class
Public Class Train
Inherits Transportation
Public Overrides Function Move() As Boolean
‘code goes here
End Function
End Class
Interface

• An interface is a special kind of construct


like class which contains just the
declaration of methods.
• It defines a contract and any class that
implements this interface must provide
implementation for all the methods
declared inside the interface.
.
Interface Transportation
Function Move() As Boolean
End Interface
--------------------------------------------
Public Class Bicycle
Implements Transportation
Function Move() As Boolean Implements
Transportation.Move
‘code here
Move = True
End Function
End Class

Das könnte Ihnen auch gefallen