Classes and Objects
If you are new to VB.Net School
This is the 4th lesson in the series of the VB.Net School. The VB.Net School is a kind of interactive learning platform where those who want to learn .NET with VB.Net can find help and support. With one issue a week, describing some areas of the VB.Net Programming Language with the Microsoft .Net Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated
message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author (
farazrasheed@acm.org) or the WEBMASTER (
info@programmersheaven.com).
For previous lessons click
here
Lesson Plan
Today we will start Object Oriented Programming (OOP) in VB.Net. We will start with learning classes, objects and their basics. Then we will move to constructors, access modifiers, properties, method overloading and Shared members of a class.
Concept of Class
A class is simply an abstract model used to define new data types. A class may contain any combination of encapsulated data (fields or member variables), operations that can be performed on the data (methods) and accessors to data (properties). For example, there is a class String in the System namespace of .Net Framework Class Library (FCL). This class contains an array of characters (data) and provide different operations (methods) that can be applied to its data like
ToLowerCase(),
Trim(),
Substring(), etc. It also has some properties like
Length (used to find the length of the string).
A class in VB.Net is declared using the keyword Class and its members are enclosed with the
End Class marker
Class TestClass
' fields, operations and properties go here
End Class
Where
TestClass is the name of the class or new data type that we are defining here.
Objects
As mentioned above, a class is an abstract model. An object is the concrete realization or instance build on a model specified by the class. An object is created in memory using the
'New' keyword and is referenced by an identifier called a "reference".
Dim myObjectReference As New TestClass()
In the line above, we made an object of type
TestClass that is referenced by an identifier
myObjectReference.
The difference between classes and implicit data types is that objects are reference types (passed by reference to methods) while implicit data types are value types (passed to methods by making a copy). Also, objects are created at heap while implicit data types are stored on a stack.
http://www.programmersheaven.com/articles/faraz/lesson4_img1.gif
Fields
Fields are the data contained in the class. Fields may be implicit data types, objects of some other class, enumerations, structs or delegates. In the example below, we define a class named Student containing a student's name, age, marks in maths, marks in English, marks in science, total marks, obtained marks and a percentage.
Class Student
' fields contained in Student class
Dim name As String
Dim age As Integer
Dim marksInMaths As Integer
Dim marksInEnglish As Integer
Dim marksInScience As Integer
Dim totalMarks As Integer = 300 ' initialization
Dim obtainedMarks As Integer
Dim percentage As Double
End Class
You can also initialize the fields with the initial values as we did in the
totalMarks in the example above.
If you don't initialize the members of the class, they will be initialized with their default values. The default values for different data types are shown below
| Data Type | Default Value |
|---|
| | |
| Implicit data types | |
| Integer | 0 |
| Long | 0 |
| Single | 0.0 |
| Double | 0.0 |
| Boolean | False |
| Char | '\0' (null character) |
| String | " " (an empty string) |
| Objects | Nothing |
Methods - Sub Procedures and Functions
Methods are operations that can be performed on data. A method may take some input values through its parameters and may return a value of a particular data type. There are two types of methods in VB.Net: Sub Procedures and Functions.
Sub Procedure
A sub procedure is a method that does not return any values. A sub procedure in VB.Net is defined using the Sub keyword. A sub procedure may or may not accept parameters. A method that does not take any parameters is called a parameterless method. For example, the following is a parameterless sub procedure
Sub ShowCurrentTime()
Console.WriteLine("The current time is: " & DateTime.Now)
End Sub
The above sub procedure takes no parameters and returns nothing. It does nothing more than print the Current Date and Time on console using the
DateTime Class from the
System namespace. A sub procedure may accept parameters as in the following example
Sub ShowName(ByVal myName As String)
Console.WriteLine("My name is: " & myName)
End Sub
Above, the ShowName() sub procedure takes a String parameter
'myName' and prints this string using the
Console.WriteLine() method. The keyword
'ByVal' with the
myName parameter represents that the String
myName will be passed to the method by value (i.e., by making a copy of the original string)
Functions
A function is a type of method that returns values. A function, like a sub-procedure may or may not accept parameters. For example, the following function calculates and returns the sum of two integers supplied as parameters.
Function FindSum(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Dim sum As Integer = num1 + num2
Return sum
End Function
We defined a function named
FindSum, which takes two parameters of
Integer type (num1 and num2) and returns a value of type
Integer using the keyword
return.
School Home