Dont Understand Classes.

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,016
Location
Cambridge, UK
Programming Experience
5-10
Dont Understand Classes. [RESOLVED]

I know this is going to sound terrible but i have no clue about classes. I've read mane articles on how using classes is beneficial, and that creating your own can speed up development time. However i just dont understand how they work. Upon trying to make my own i just stare a the empty class and wonder what i'm going to put in there. I realise i need to get my head round this or i'll never really understand .Net. Can Anyone please help, Or konw of any really simple tutorials, (by simple i mean REALLY simple!!)
 
Last edited:
Classes/Structures are the backbone of .Net. Here's a quick overview followed by some good references:

Classes are reference types. On instantiation (when the constructor 'new' is run), they are created in the heap and a pointer is placed on the stack referencing them. These are for heavy objects that will stick around for the most part of a programs lifecycle.

Structures are value types. They don't need to be instantiated because they exist soley on the stack. These are designed for light weight objects that are only used temporarily (ie, in a method and won't be used when they go out of scope).

Both of these objects can contain methods, properties and values. How they exist in memory is the only difference.

Both can be nested. Meaning, classes can contain classes/structures and vice versa.

Tutorials/Webcasts:
This is a great collection of webcast series on everything .Net related. At the bottom, there's a 15 part series on VB.Net. At the top, there's another series about Modern Software Architecting. The bottom series will teach you the 'what' and 'how,' the top will teach you the 'when' and 'why.'
http://www.microsoft.com/events/series/modernsoftdev.mspx
 
a class combines data, functions, and types into a new type. Microsoft uses the term type to include classes.
VB.NET:
Public Class ClassName

End Class

In VB .NET, class modules can contains
Data members
This includes member variables and constants.

Event members
Events are procedures that are called automatically in response to some action that occurs.

Function members
This refers to both functions and subroutines, is also called a method.

Property members
A property member is implemented as a Private member variable together with a special type of VB function that incorporates both accessor functions of the property.

Type members
A class member can be another class, which is then referred to as a nested class.

A class is just a description of some properties and methods and does not have a life of its own .To execute the methods and use the properties of a class, we must create an instance of the class, officially known as an object.

ways to instantiate an object of a VB .NET class.
VB.NET:
Dim Anis As class1

and then instantiate the object using the New keyword as follows:
VB.NET:
Anis = New Class1( )

can combine these
VB.NET:
Dim Anis As New Class1( )

Dim Anis As Class1 = New Class1( )
 
VB.NET:
Public Class Class1

' -------------
' Data Members
' -------------
' Member variables
Private aName As String
Private aAge As Integer

' Member constant
Public Const myAge As Short = 120

' Member event
Public Event Testing( )

' ----------------
' Function Members
' ----------------
' Method
Public Sub Test( )
RaiseEvent Testing( )
End Sub

Property Age( ) As Integer
Get
Age = aAge
End Get

Set(ByVal Value As Integer)
If Value < 0 Then
MsgBox("Age cannot be negative.")
Else
Age = Value
End If
End Set
End Property

' Property
Property Name( ) As String
' Accessors for the property
Get
Name = aName
End Get
Set(ByVal Value As String)
aName = Value
End Set
End Property

' Overloaded constructor
Overloads Sub New( )

End Sub

' Constructor that initializes name one argument
Overloads Sub New(ByVal NewName As String)
Name = NewName
End Sub

Sub Dispose( )
' Code here to clean up
End Sub

End Class
 
Good example. I just would like to clear up a couple things.

Although it's not necessary, properties still should have a scope associated. Anything that can have a scope, should have a scope. Also, they normally "return aAge" instead of working like a function.

The 'New' doesn't need to be overloaded.
Dispose does.

Lastly, Msgbox is legacy code. We're supposed to be using "MessageBox.Show"

I don't mean to nit-pick. All of it will work fine (besides the constructor), but since it's an example for someone who's not familiar with classes; I thought I'd throw in my 2 cents.
 
Thanks so much for all the responses. I am starting to understand a little bit. So would i be right in saying that the constructor i.e

Sub New()
End Sub

Anything in there is run once and only once when the class is instantiated?

Also the properties, do they always have to be serialized inside the code and if not where are the values saved. Mmm.... the more i write the more i think i'm not getting this at all.
 
You actually have to instantiate the type as 'new' and you can run it as many times as you feel like. Each time it will create a new instance of the object on the heap and change/add the reference on the stack.

This creates a reference on the stack:
public example as class1

This is the actual instantiation
example = new class1(param1,param2,...)

You can do both on the same line as well (there's no performance penalty like there was in vb6 for this):
public example as new class1(param1,param2,..)

Properties are basically a subroutine(set) and a function(get) rolled into one (for lack of a better term). A property itself doesn't hold a value.

If you look at aniskhan's example of a property, on get; it returns the value of 'aAge.' This can be done two ways:

(I added the ReadOnly for simplicity sake, it just means that referencing components aren't able to "Set" using the property)
VB.NET:
Public ReadOnly Property Age( ) As Integer
  Get
    Age = aAge
  End Get
End Property
VB.NET:
Public ReadOnly Property Age( ) As Integer
  Get
    return aAge
  End Get
End Property
The property itself is never a value. It's a means to work with values from inside your class.

Is this making sense?
 
Ok i think i'm beginning to understand why classes are so important and a basic idea on how to use them. Thanks for all the help, it is very much appreciated.
 
Back
Top