Results 1 to 10 of 10

Thread: What Are class modules, and how are they diffrrent from regular modules?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    well... the question is in my title


    NXSupport - Your one-stop source for computer help

  2. #2
    Guest
    Classes differ from standard modules in the way their data is stored. There's never more than one copy of a standard module’s data. This means that when one part of your program changes a public variable in a standard module, and another part of your program subsequently reads that variable, it will get the same value. Class module data, on the other hand, exists separately for each instance of the class (that is, for each object created from the class). By the same token, data in a standard module has program scope — that is, it exists for the life of your program — while class module data for each instance of a class exists only for the lifetime of the object; it’s created when the object is created, and destroyed when the object is destroyed.
    Finally, variables declared Public in a standard module are visible from anywhere in your project, whereas Public variables in a class module can only be accessed if you have an object variable containing a reference to a particular instance of a class. All of the above are also true for public procedures in standard modules and class modules

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    thanks megatron
    NXSupport - Your one-stop source for computer help

  4. #4
    New Member
    Join Date
    Aug 2000
    Posts
    3
    Another difference is that you can raise events from a class, but cannot from a module.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    thanks
    NXSupport - Your one-stop source for computer help

  6. #6
    Guest
    i suppose you could think of a class module as a User-Defined Type, which has its own functions and subroutines.

  7. #7
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I think megatron's highlighted the main differences but I would quite like to add a little more, the first question people usually have about class modules is "what's the point in having a module that you have to declare an instance of and can only get at its functions if you have an instance of it declares in the current scope?" which is a good question. the key point is that class modules are about mixing your data and your code together. let's have an example

    Say you are working with angles and you and the user like to measure angles in degrees and minutes (a minute is a 60th of a degree) but VB works in radians (VBs trigonomic functions take their arguments in radians) , how do you solve this problem?

    one way would be to have a standard module with functions that convert degreea and minutes to radians and vice versa and one for converting degrees and minutes into a string (so you can display it to the user) and one that converts radians into a degrees and minutes string etc. but this way you would be calling these functions all the time and your code would be absolutley cluttered with calls to these functions, not only is is hard to write but it soon becomes almost unreadable and very hard to debug.

    no prizes for guessing that the solution is a class module.

    so what we are going to do is have a class module that represents an angle, it stores the value of this angle and all it's functions work on this angle (ie they don't take the input parameters that the module would have to, instead of having a radians function which takes degrees and minutes as parameters the class has a radians property which returns/sets the value of the angle)

    now by a mind blowing coincidence I happen to be working on a project that involves a class just like this one. Here's the code.

    Code:
    Option Explicit
    
    
    Dim m_Degrees As Integer
    Dim m_Minutes As Integer
    
    
    
    Public Property Let Degrees(New_degrees As Integer)
    
    'we have to do the mod function twice to catch -ve numbers
    m_Degrees = ((New_degrees Mod 360) + 360) Mod 360
    End Property
    
    Public Property Get Degrees() As Integer
    Degrees = m_Degrees
    End Property
    
    
    Public Property Let Minutes(New_Minutes As Integer)
    m_Minutes = New_Minutes Mod 60
    Me.Degrees = Me.Degrees + Fix(New_Minutes / 60)
    End Property
    
    Public Property Get Minutes() As Integer
    Minutes = m_Minutes
    End Property
    
    
    'This is the Value of the Angle in Radians
    'It is set as the Default Property.
    'This allows us to add angles and perform trig operations on it directly
    'It is hidden to keep syntax consistent
    Public Property Let Radians(New_Radians As Double)
    Me.Degrees = 0
    Me.Minutes = (New_Radians * 10800 / PI)
    End Property
    
    Public Property Get Radians() As Double
    Dim dblTemp As Double
    
    dblTemp = Me.Degrees + (CDbl(Me.Minutes) / 60)
    
    Radians = dblTemp * PI / 180
    
    End Property
    
    
    Public Property Get StringValue() As String
    If Me.Minutes Then
    
        StringValue = Me.Degrees & "°" & Me.Minutes & "'"
    
    Else
    
        StringValue = Me.Degrees & "°"
        
    End If
    End Property
    (N.B. I have PI declared as a public constant in a standard module)

    ok statrt a new project add a class module to it call it clsAngle and pase the code in

    before you do anything else go to the tools menu, his procedure attributes, select radians from the combo box, hit advanced and turn on the user interface default check box

    now you have an angle variable you can treat like a normal variable but it's much better, try some code like this to see what I mean

    Code:
    Dim alpha As New clsAngle 'NB, classes used as variables like this  have to be declares with the new keyword
    
    Dim beta As New clsAngle
    
    'set alpha to 60.5 degrees
    alpha.Degrees = 60
    alpha.Minutes = 30
    
    'set beta to 29.5 degrees
    beta.Degrees = 29
    beta.minutes = 30
    
    
    Msgbox "alpha = " & alpha.StringValue & ".  Sin(alpha) = " & Sin(alpha)
    
    Msgbox "beta = " & beta.StringValue & ".  Sin(beta) = " & Sin(beta)
    
    
    alpha = alpha + beta
    
    Msgbox "Sin(" & alpha.StringValue & ") = " & Sin(alpha)
    so we can treat an instance ofour class module like a variable (because we set the radians property as the user interface default we can automaticly access the radians property, which allows us to add the angles together and plug them into trigonomic formulae very easily)

    so that's the difference between a class module and a standard module.ik

  8. #8
    Guest

    Thumbs up Use classes for tables as well

    Ok if you have a table called Customer which is used over multiple projects, forms, modules etc. Rather than loading the table at each iteration, writing coding to test updates etc, create a customer class. Chuck all your update rules into, and you only have to define the table once.

    So for example customer name is a required field. You would have a property called "name", and a method called "update"
    which checks that name has a value

    Therefore programmatically:
    Code:
    customer.name = txtname
    customer.update
    would handle it all for ya. Get into it, will save you alot of coding time.

    Big advantage.....if there is a bug in a table update, it can only occur in one place.

  9. #9
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    An ActiveX control without a user interface, and is created at runtime, not design time. (vb5)

    Can you create an ActiveX control (or an intrsinc control) at runtime (and create the user interface on the form) in vb6?

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    set youobject=createobject(classname)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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