Results 1 to 3 of 3

Thread: Please help with CLASS

  1. #1

    Thread Starter
    Addicted Member MegaMan's Avatar
    Join Date
    May 2002
    Posts
    128

    Angry Please help with CLASS

    Hello all you intelligent programmers!

    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!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:

    VB Code:
    1. 'declare private variables to hold internal values
    2. Private m_Name as string
    3. Private m_LoggedIn as boolean
    4.  
    5. 'then expose them through properties
    6. Public Property Let Name(NewValue as string)
    7.    m_Name=NewValue
    8. End Property
    9.  
    10. Public Property Get Name() as string
    11.    Name=m_Name
    12. 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.

    VB Code:
    1. Public Sub WhoBarked()
    2.    MsgBox m_Name & " barked! " 'references the private variable
    3. End Sub
    4.  
    5. 'using it like this
    6. CurrentUser.Name="Bob"
    7. 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

    VB Code:
    1. 'in class
    2. Public Event LoggedOff()
    3.  
    4. Public Sub LogOff() 'of course normal a sub wouldn't really raise an event
    5.    'perform log off here
    6.  
    7.    'raise event
    8.    RaiseEvent LoggedOff
    9. End Sub
    10.  
    11.  
    12. 'in app
    13. Private WithEvents CurrentUser as cUser
    14.  
    15. Private Sub Form_Load()
    16.   Set CurrentUser=New cUser
    17. End Sub
    18.  
    19. Private Sub Form_Unload(Cancel as integer)
    20.   Set CurrentUser=Nothing
    21. End Sub
    22.  
    23. Private Sub CurrentUser_LoggedOff()' there is no need to create this it will show up when the object is declared withevents
    24.    Msgbox "Hey you fired an event"
    25. End Sub

  3. #3

    Thread Starter
    Addicted Member MegaMan's Avatar
    Join Date
    May 2002
    Posts
    128

    Cool thanks! (revised)

    you rock. that was the kind of reply i was looking for! two tequila shots for you!!

    and the best part is it worked!!!
    Last edited by MegaMan; May 1st, 2002 at 11:33 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width