Results 1 to 3 of 3

Thread: Quick Question, Please Help!

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 1999
    Location
    metairie, la usa
    Posts
    40

    Post

    HI,
    I have been programming in visual basic for about 2yrs. I am all self taught and I have never used the class module before. I know that a regular module can be used to declare global sub/funtion/vars/api declares, but what exactly does the class module do and what are the major differences. Thanks

    Edited by winapi on 03-12-2000 at 07:20 PM

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089

    Post

    Class Modules are completly Different to modules.
    I have to stress this Because I got the 2 Confused for a while, although for some reason I knew about class modules before modules. (Probably someting to do with my boss being a class module fanatic)

    Basicly a class module is used to store and manipulate data in one object.

    They have properties, methods and events much like user controls but you cant use them at design time and you cant put them on a form or see them.

    I'm a bit stuck for simple examples
    but say you're programming a **** roleplaying type game and you want to keep track of a characters Money health etc
    (Please don't think i devote any of my time to roleplaying games of any sort but this is the only example I can think of that uses most of a class modules capabilities)

    you could have a class module representing a character.

    This is a quick example with

    Properties:

    Name as string
    Money as long
    Health as byte 'From 0 to 100, 0 means he's dead

    Methods:

    Hurt(Amount as byte, Randomize as boolean)
    'Takes away a characters health if randomize is selected it 'picks a random amount to take away from 0 to Amount

    Cure(amount as byte, Randomize as byte)
    'Similar to Hurt but adds to a characters healh instead
    'of taking it away

    Events:

    Dies Character is dead

    The Code Would Be as Follows

    Code:
    Option Explicit
    
    Dim propMoney As Long
    Dim propHealth As Byte
    Dim propName As String
    
    
    Event Dies()
    
    
    Public Property Let Money(Data As Long)
    If Not Data < 0 Then propMoney = Data
    End Property
    
    
    Public Property Get Money() As Long
    Money = propMoney
    End Property
    
    
    
    Public Property Let Health(Data As Byte)
    propHealth = Data
    End Property
    
    
    Public Property Get Health() As Byte
    If Data > 0 And (Not Data > 100) Then Health = propHealth
    End Property
    
    
    
    Public Property Let Name(Data As String)
    propName = Data
    End Property
    
    
    Public Property Get Name() As String
    Name = propName
    End Property
    
    
    Public Sub Hurt(Amount As Byte, Randomize As Boolean)
    
    Dim intNewHealth As Integer 'Integer in case health is negative
    
    If Randomize Then
    
        intNewHealth = propHealth - Fix(Rnd * (Amount + 1))
    
    Else
    
        intNewHealth = propHealth - Amount
    
    End If
    
    If Not intNewHealth > 0 Then
    
        propHealth = 0
    
        RaiseEvent Dies
        
    Else
    
        propHealth = intNewHealth
        
    End If
    
    End Sub
    
    
    Public Sub Cure(Amount As Byte, Randomize As Boolean)
    
    Dim intNewHealth As Integer 'I just copied the hurt code and changed some signs so we'll still use the integer
    
    If Randomize Then
    
        intNewHealth = propHealth + Fix(Rnd * (Amount + 1))
    
    Else
    
        intNewHealth = propHealth + Amount
    
    End If
    
    If intNewHealth > 100 Then
    
        propHealth = 100
        
    Else
    
        propHealth = intNewHealth
        
    End If
    
    End Sub
    
    Private Sub Class_Initialize()
    
    propName = ""
    propMoney = 0
    PropHealth = 100
    
    End Sub
    you only need to do the code once and you have a variable which represents a character and handles all the code needed for him which keeps the code out of your standard modules and keeps everything nice and readable

    you can have more than one instance of a class running at the same time with different property values for each

    declare them with

    Dim objDaveTheFictionalCharacter as New clsCharacter
    'ClsCharacter is what you called your class

    and get rid of them with

    Set objDaveTheFictionalCharacter = Nothing

    You can pass these to Procedures, Return them from functions Stick them in collections and everything as long as you remember

    1 classes need to be initialized either by declaring them as
    Dim objWhatever as new clsWhatever

    or by using
    Set objWhatever = New clsWhatever

    2.
    Copy a class to a new variable with

    Set objWhatever2 = objWhatever

    3.
    Whenever you pass classes to procedures or put them in new variables they are passed by reference so both variables access the same data so if you change a property in one dont expect it to be the same in the other (This can be veryfrustrating or you can use it to your advantage, Your choice)

    3,. Terminate a class with
    set objWhatever = Nothing

    4. If you have 2 variables pointing to the same instance of a class and you set one to nothing the object will still be running and you can still access it from the other variable, it's only when all references to that instance terminates.

    Hope his Helps

  3. #3
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141

    Post

    The difference between a regular every day module
    and a class module can be summed up this way:

    A regular module can have Subs and Functions that you
    call in your application. You're familiar with this
    type so I won't say much else about this.

    A class module also has Subs and Functions as well as
    Property Let and Property Get declarations. The Subs
    and Functions of a Class Module become the Events and
    Methods of the Class. IE. The Click event of a button
    class or the AddItem Method of a ComboBox Class.

    The Property Let and Property Get parts become the
    Properties of the class. IE. On a label control, you can
    set the Caption Property to anything you want using the
    Label1.Caption = "Hi There". This is actually using the
    Property Let of the Label Class to set this value for the
    Caption Property.

    By using a class module, you're just building your own
    class of objects where you get to choose which properties,
    events and methods you want to make available to the
    application via your Subs, Functions, and Property Let/Get.


    Hope this helped.
    JC

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