Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

classes and member

access modifiers
Functions and Sub procedures /
Programs in VB .NET (Methods)
 Functions and sub procedures must either have a sub or function keyword in the function or
sub procedure heading
 They can be either public or private (following the same conventions in OOP languages such as
C++ or Java)
 All parameters are passed by value by default in VB .Net
 Parameters of any data type may be passed by reference
 All Objects and arrays are passed by reference not value
 All events are handled by a sub procedure in VB .Net
 Functions return a value and Sub Procedures do Not
Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Dim result As Integer

If (num1 > num2) Then


result = num1
Else
result = num2
End If
FindMax = result
End Function
Sub CalculatePay(ByRef hours As Double, ByRef wage As Decimal)
'local variable declaration
Dim pay As Double
pay = hours * wage
Console.WriteLine("Total Pay: {0:C}", pay)
End Sub
CLASSES AND OBJECTS
 A class is similar to a container that may have datamembers such as
variables, constants, methods, properties ,events , constructors etc.,
 An object is an instance of a class

[access-modifier] class <classname>


variables-declaration
methods declaration
End Class

class Class1
Dim x as integer
Public y as integer
End Class
CLASS IN VB.NET

 [ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable


] [ Partial ] _
 Class name [ ( Of typelist ) ]
 [ Inherits classname ]
 [ Implements interfacenames ]
 [ statements ]
 End Class
Where,
 attributelist is a list of attributes that apply to the class. Optional.
 accessmodifier defines the access levels of the class, it has values as - Public,
Protected, Friend, Protected Friend and Private. Optional.
 Shadows indicate that the variable re-declares and hides an identically named
element, or set of overloaded elements, in a base class. Optional.
 MustInherit specifies that the class can be used only as a base class and that you
cannot create an object directly from it, i.e., an abstract class. Optional.
 NotInheritable specifies that the class cannot be used as a base class.
 Partial indicates a partial definition of the class.
 Inherits specifies the base class it is inheriting from.
 Implements specifies the interfaces the class is inheriting from.
Access Modifiers

 Public
 Private
 Protected
 Friend
 ProtectedFriend
FUNCTION AND SUB PROCEDURES

[access-modifier]Function <function-name>(parameterlist)
As type

[access-modifier]sub <sub-procedure-name>(parameterlist)
As type
Sub procedure body
End Sub
Creating Objects

Dim <objectname> As New <ClassName>()


Objectname.variablename
Objectname.methodname()
Constructors and destructors

 Constructors and destructors are special types of methods


 A constructor is a method that is called when the object is
created
 A destructor is a method that is called when the object is
destroyed.
 In visual basic we use New method to create a constructor
and Finalize or Dispose methods are used to call a destructor
 The main features of a constructor are-
 It is a sub procedure declared with a New keyword
 It does not have any return type
 It is invoked automatically when the object is created.
Partial classes
 A class definition consists of a set of member variables and methods
 The member variables are declared within a class body.
 However you can break the class into two or more classes where
each class is known as partial class and stored in separate source
file.
 These definitions in different source files are combined when they
are executed.
 A class is divided into two or more classes using the partial
keyword.
Shared members

 Sharing a member of a class or a structure makes it available to every


instance of a class and structures
 We donot need to create an object instead we can call it using the classname
dotoperator and the method name

You might also like