Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to user classes 1

Status
Not open for further replies.

Corinne

Programmer
May 15, 2001
146
US
Hello,

I want to begin to understand classes and begin to use them. I'm not sure exactally where & how they are used. What is the main difference between classes and modules? Can anyone give me an explanation of them? Thanks in advance.

Corinne
 
Hi there,

class modules and standard modules are way apart. I will make the assumtion that you already know what the standard module comprises of, i.e. Functions and subroutines.

1st an explanation then a demo at the bottom :)

OBJECTS:
Classes define objects, so looking at yourself as an object you have properties, like air colour, height, weight etc. You also have methods, walking, talking, eating etc. Therefore you are an object; where the class comes in is that it defines what an object is.

CLASS:
Working on the latter section we take yourself as an object and strip away the values stored in your properties and methods. The class doesnt say what colour your hair is, it just says that you have hair. The class dosnt say how fast you are walking, it just says there is a method that you can accomplish called walking. The age old analogy that is used is a class is a cookie cutter for an object. It tells it what shape it is but not what it is made of or looks like.

CREATION:
In VB you can create a class in the IDE by adding a class module to your project, or by using the class builder, however thats another story! But one which is a great help! Anyhow, you add a class module. and there you have it, an empty class module.

PROPERTIES:
Now to give it some properties these are simply declarations i.e.
Public strMessage as string

METHODS:
And finally the method! in this case a simple subroutine.
private sub subDisplay()
msgbox strMessage
end sub

and there you have it a class created! So far you have a class which we will name clsMessage which has a property of strMessage and a method of subDisplay. Now to call the class and see it working:

open vb and start a standard exe, now you have a form, now add in a class module. In the class module insert the following code

Public strMessage as string

private sub subDisplay()
msgbox strMessage
end sub

Now go back to the form and double click it, the code needs to look like below:

Private objNewMessage As clsMessage
Private Sub Form_Load()
Set objNewMessage = New clsMessage
End Sub

Finally select the class module and in he properties window call it clsMessage. Now run the program; pause it and open the immediate window.

type in the immediate window

objNewMessage.

and when you should get the tag suggestor suggesting a method of subDisplay and a property of strMessage, so set the property to something.


objNewMessage.strMessage = "Hello World"

then again back to the object and call its method

objNewMessage.subDisplay

voila!!! you should have a messagebox.

What has happened is that the form has created an instance of the class, you have then gone on to creat the object as a whole by assigning it a property and executing a method. You can have more than one instance of these running so lets say you wanted three text boxes you simply do the declarations in the form code however many times you like so you could have

Private objPreviousMessage As clsMessage
Private objThisMessage As clsMessage
Private objNextMessage As clsMessage
Private Sub Form_Load()
Set objPreviousMessage = New clsMessage
Set objThisMessage = New clsMessage
Set objNextMessage = New clsMessage
End Sub


Here endeth the story of the class and the module :eek:) Only for one simple reason, its midnight. Theres loads more and i could go on for days on end becuse OO rocks, even though VB isnt really OO the classes and collections certainly make it worth doing.

I shall let you ponder over that and get your creative juices going and leave you with some other words to have a shiftie at to do with classes

collections (collections for classes)
inheritance (of classes)
terminate (event that all classes have)
initialize ( ditto )

like i say, theres loads more but you get the idea. Owt else then drop me a line.

cheers

'mi casa es su casa'
]-=tty0=-[
ICQ:82621399
 
tty0,
When I try the code as explained above it doesn't work unless I change the Private Sub subDisplay() to Public Sub subDisplay() in the clsMessage Class Module, so I wanted to make sure that I am correct.
Thanks,
vbdrk
 
You are absolutely right vbdrk, "Private" means it cannot be accessed from outside of the class. In order for that function to work, it had to be changed to "Public".

Be an idiot:
 
Ponerse, Thanks for the quick response as I am just trying to learn class modules as I found some code I needed, but it was in a VB class file. In order for me to get it to work, I have my work cut out for me :) so I am starting with the basics and moving up from there. The class file I have has comments in it but no real docs and it is kinda big. Thats the best way to learn sometimes...
I appreciate the response,
vbdrk
 
some of the third party tutorials have 'decent' chapters on cllasses and class modules. If you do not have one of these, I suggest a thoughtful visit to ye olde locale booke storee. if you really want to 'grow' in VB(A), documentation/tutorials from some of these is quite helpful.



MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Hi all,

thanks pon, very true and not sure why I typed that up like I did. and apologies to vbdrk and anyone that had tried and wondered why it doesnt work.

peace
chris

'mi casa es su casa'
]-=tty0=-[
 
MichaelRed: Can you suggest a good one? I have all kinds of books here and have no problem buying more. Most of my books seem to have the chapter in the advanced section in the back of the book and sometimes, you just need a very simple example or two until it sinks in :) tty0 has done that here and it makes it easy to understand whats happening when you step through it...Thanks in advance.

tty0: Thanks for the great example, even with the extra credit built in (homework).

vbdrk
 
I would recommend:

Visual Basic 6 Super Bible by The Waite Group
 
DrJavaJoe:
I just browsed through my library and I have that book! I guess I should read it huh? Sometimes I have the tools but don't know it :) Seriously though, I have looked through a bunch of my books but the ones I checked first must have confused me right away, and then it didn't get any better...I will read about in in the Super Bible and see how I get on from there.
Thanks,
vbdrk
 
vbdrk: I have at least 10 books on VB, but whenever I have a problem or question I end up searching for it in newsgroups. The books do make me look smart though.

Be an idiot:
 
I learnt it a strange way, one sunny drunken afternoon in a pub garden putting the world to rights and someone took time to sit down and explain it in laymens terms. However I dont suggest that you sit in a pub garden awaitiing enlightenment. Mind you its an excuse!

For a book although its a bible kinda thing and not VB I have revisions of ISBN 0 07 709677 0 which scoots over the principals in an easy way.

Or if you want to get into a bit of decent easy OOP try ruby, just like small talk ruby is pure OO the number 177 is an object and has a lenth and a value etc, even a colour if you wish. Plus its multithreaded with a nice API.


happy coding!


'mi casa es su casa'
]-=tty0=-[
 
ponerse: I must really look smart then as I have about 40 books on VB :) The problem is that they are great references but it takes so long to figure out which book has the info you need at the time, that you get sick of looking, and get on the internet. Each book seems to have good info but none have all the info...
vbdrk
 
tty0, Thanks for the links. I will download Ruby later as it seems to be slow right now. I will check it out though as it looks interesting. As far as the sunny afternoon in a pub method of learning...I am always reading my books every chance I get, whether it makes sense or not. Then sometimes I even have dreams of the code I am struggling with. Then some days I will just seem to go to work on an app and it seems like I know how to do things I didn't understand before :) I guess they call it "timed delay learning". It probably just takes time to soak in...
vbdrk
 
Your welcome,

It all falls into place in the end.

:eek:)

'mi casa es su casa'
]-=tty0=-[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top