|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Classes and Instances ( Objects )
| Everything in OOP ( Object Oriented Programming ) is centered around objects.
Objects are constructs in which a state and some behaviour ( actions involving this states ) is combined.
Objects can represent concrete subjects ( for instance persons ) or more abstract things ( for instance reservations ).
Every object is an instance of a certain class ( in case of "single classification" ).
Classes describe objects by stating which information ( state ) objects of this type can contain and which behaviour objects of this type have. In a class you describe how a representation of a concept is made, this is what is often referred to as the abstraction process.
The easiest way to analyse a certain problem domain, or to design a solution for a certain problem, is often to speak of it in terms of concepts. Every concept and its characteristics/properties and what should be able to happen with this concept is described.
A application for invoices involves concepts like article, invoice-item and invoice. The concept article has a property prize. The concept invoice-item has a article and an amount ( of this article ). The concept invoice has a collection of invoice-items. An invoice-item must be able to deliver its total ( unit price x amount ). An invoice must be able to deliver its total ( total of invoice-items ), also including tax.
Using this higher level of abstraction, object oriented programming allows us to easily define the solution for this problem domain. A class is in fact perfect for describing a concept with its properties and behaviours.
When used correctly an object orientated approach should lead to more reusable, better maintainable and better scalable code. |
Up
Our First Class
| Following example is an simple abstraction of a person. A person has a name property. Each object of this class can hold its own value for the name property. |
| Class Person
Private m_Name As String
Public Function GetName() As String
GetName = m_Name
End Function
Public Sub SetName(ByVal value As String)
m_Name = value
End Sub
End Class Download Broncode |
| To define a class use the Class ... End Class-block.
The keyword Class is followed by the identifier of the class. Try to describe in the identifier of this class what an object of this class represents. In this example an object of this class would represent a person, so Person is a suitable name. |
Up
Members
| Three members are defined : (1) a encapsulated ( Private ) field to store the name-value of a person (2) a get-function to allow clients ( Public accessible ) to query the person object for its name (3) a set-procedure to allow clients ( Public accessible ) to give the person object the command to set its name value |
Up
Methods - Commands and Queries
| Functions ( "queries" ) are usually used to query the object for information ( a base or derived state ).
Procedures ( "commands" ) are usually used to manipulate a state. |
Up
Access Modifiers
| Access modifiers Private and Public define the form of access to this members. All Public members are accessible for clients of the class. All Private members are only accessible within the class. All members the clients need to work with are made public, and added to the public interface of this class. All other members, only needed for implementation purposes, can be encapsulated ( made private ). Often a encapsulated field ( used to store some state ) ( like m_Name ) is combined with public members ( like GetName and SetName ) allowing the state to be queried and/or set. |
Up
Naming Guidelines
| The following guidelines for identifiers of classes and its members are used : - prefix m_ ( m stands for "memory" ) on field identifiers - "PascalCasing" for identifiers of classes and public members - "camelCasing" for encapsulated members In "PascalCasing" all first character of each word in a combined word is a capital, for instance SomeMember. "camelCasing" is similar, but the first character is a not a capital , for instance someMember. |
Up
Creating Instances of Class
| Module Client1
Sub Main()
Dim person1 As New Person
person1.SetName("John")
Console.WriteLine(person1.GetName())
Dim person2 As New Person
person2.SetName("Jane")
Console.WriteLine(person2.GetName())
Console.ReadLine()
End Sub
End Module Download Broncode |
| In the above client you can see how the Person class is used. An instance is created, and the reference to that instance is stored in a variable (4). The name property of the object is set (5) and queried (6).
All public members of the object are accessible by using the "dot notation". The identifier is followed by a dot and the identifier of the public member.
All members are object related ( instance related ). For instance when setting the name of person1 ( person1.SetName(...) ) only the name of person1 ( and not person2 ) is changed.
Line (4) is a shortened notation for Dim person1 As Person = New Person. The New Person part is the "object initializer", which consists of the New keyword followed by the identifier of the class to instantiate. This object initializer is also an expression of the type that the class defines. The expression evaluates to a reference of the created object. This reference is then assigned to the variable person1, also declared of that type. This variable is needed to access the instantiated object. |
Up
Reference Type and Value Type
| By defining a class a referencetype is constructed. Other types we've used before, like 'String' or arraytypes are also defined by as class. Remember that in contrary to the value types ( like Integer and Char ) we explicitly need to instantiate the type to create an instance. And that variables of a referencetype can only contain a reference to an object of Nothing ( no reference ). A simple declaration of a referencetype variable will associate that variable with Nothing. No instances are created by declaring a reference type variable. Only when we assign a reference of an object to that variable, the object can be accessed thru that variable.
A declaration of a value type variable on the other hand will immediately associate that variable with an instance. |
Up
Nothing ( Null ) and NullReferenceExeception
| When the following example is executed, a NullReferenceExeception ( object reference not set to an instance of an object ) occurs.
An exception is an runtime error.
The variable person1 contains Nothing ( also called Null within .NET ) because no reference of any object is assigned to the variable. Calling the instance related member on this variable will generate an error, because it is not know on which object the implementation of that members should be executed. |
| Module Client2
Sub Main()
Dim person1 As Person
person1.SetName("John")
End Sub
End Module Download Broncode |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|