The Basics to classes

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
Could someone please post a small example of a working class?

I am trying to do one at the moment, but i really dont know enough about them.

Thanks.
 
This is a simple class that holds data, and has one function to show you use. The function just returns the name of the object (its a document) in uppercase. There are LOADS of excellent reasons to use classes, but just think of each class as a new box.
You stick stuff in it (the private variables normally) and then ask it for data back. You can tell the box to do something to the data, like return in upper case, or run functions based on the data.
Its also good for passing it data in one format (ie a string) and it can convert to another, or using properties, you can allow an integer to be added to the class, but only if its greater then zero.

If your just getting into how classes work, then keep away from inheritance, overriding, overloading and scope of things until you know how (and where) to use them.

Another thing to note, is that everything is really a class.
A form is a class (it inherits all its functions), a module is a class used once (singleton), controls are instances of classes.

Code:
Friend Class clsDocument

    'Unique ID
    Private pUniqueID As Integer
    'Name of document
    Private pName As String
    'Owner of this document
    Private pOwner As Integer

    Friend Sub New()
        'Set the defaults
        pUniqueID = -1
        pName = ""
        pOwner = -1
    End Sub

    Friend Function funcGetNameUpper() as String

      return pName.ToUpper()

    End Function

End Class
 
Thanks.......Good Example of the actual class - but could you please give me an example of the bit of code calling it?
 
This class is still missing a lot of features that should be present though....

Users of a class should be able to set or get the values of its members. In this case (they are private) and the constructor sets them to empty strings and -1.

Anyway, I really think you should go for MSDN, they have lots of samples because Classes is a major subject to understanding Object Oriented Programming and you should learn them the right way.

But just for demo purposes:

To use this class :
Visual Basic:
'declare a new instance of the class
Dim SampleObject as New clsDocument()
'Display the name of the document in a message box (which would show empty because the member pname is empty!!)
MessageBox.Show(SampleObject.funcGetNameUpper())

Hope this helps,
 
Brilliant.

Thank you.

Oh by the way, i like the James Dean quote - although there seems to be some confusion btw whether its 'die today' or 'die tomorrow'. I always thought it was 'die tomorrow' but a google search brings up mixed results. What a lovely car he wrote off when he did die as well. Porsche 550 Spider i believe.

Anyway, thanks again for your help :)
 
Back
Top