PDA

Click to See Complete Forum and Search --> : Please help with CLASS


MegaMan
May 1st, 2002, 07:46 PM
Hello all you intelligent programmers! :cool:

I am a new programmer-- I sort of know my way around VB but im not to familiar with module programming, dll, ocx whatever.. but i had a quick question and just to double check my understanding of how classes work. So if you know and are able to answer please do so at your earliest convenience :-)

If i created a class called clsUser and defined some parameters

Public Name as string
Public LoggedIn as Boolean

so then I could use this class to create an object like this:

dim CurrentUser as clsUser
Set CurrentUser = New objUser


CurrentUser.Name = Joe
CurrentUser.LoggedIn = True


---- furthermore i could add a sub


Public Sub Bark()
MsgBox "woof woof"
End Sub


as a procedure or method of the clsUser object and reference like:


CurrentUser.Bark
so apparently Joe barked lol..

--- i just wanted to double check and see if what i understood about classes is correct..

ok I want to know how i can use for example CurrentUser.Name and pass that value to a sub within the same class module like this maybe:


Public Sub WhoBarked(byval person as string)
MsgBox person & " barked! "
End Sub)


and access it using the following method:

CurrentUser.Name = HisName
CurrentUser.WhoBarked(HisName)


When i do this the person's name shows up as blank or null.. but if i do this:


CurrentUser.WhoBarked("Steve")

Then the output is "Steve barked!"

how do i reference a variable or the value of a property for the current instance of that object??

THANKS SO MUCH FOR YOUR HELP!

Respond
:)HEY!
:)HEY!

Edneeis
May 1st, 2002, 10:06 PM
Well you are almost right, although you can use Public Variables in a class to expose them as properties it is not recommended. The proper way is to make actual Properties:


'declare private variables to hold internal values
Private m_Name as string
Private m_LoggedIn as boolean

'then expose them through properties
Public Property Let Name(NewValue as string)
m_Name=NewValue
End Property

Public Property Get Name() as string
Name=m_Name
End Property


As for subs you can pass things in as arguments the same way you do for any other subs or you can use the internal variables within the class instead of passing anything in.


Public Sub WhoBarked()
MsgBox m_Name & " barked! " 'references the private variable
End Sub

'using it like this
CurrentUser.Name="Bob"
CurrentUser.WhoBarked 'Would show "Bob Barked"


Another thing with classes is events. You first declare them and then raise them where needed. Of course for the client to use them they will need to delcare your object WithEvents


'in class
Public Event LoggedOff()

Public Sub LogOff() 'of course normal a sub wouldn't really raise an event
'perform log off here

'raise event
RaiseEvent LoggedOff
End Sub


'in app
Private WithEvents CurrentUser as cUser

Private Sub Form_Load()
Set CurrentUser=New cUser
End Sub

Private Sub Form_Unload(Cancel as integer)
Set CurrentUser=Nothing
End Sub

Private Sub CurrentUser_LoggedOff()' there is no need to create this it will show up when the object is declared withevents
Msgbox "Hey you fired an event"
End Sub

MegaMan
May 1st, 2002, 11:00 PM
you rock. that was the kind of reply i was looking for! two tequila shots for you!! :p

and the best part is it worked!!!