Results 1 to 19 of 19

Thread: [RESOLVED] Classes

Threaded View

  1. #9
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Classes

    A UDT provides a way of grouping several variables together under one name. Generally speaking a UDT is handled just like any other variable. UDT's can be nested i.e. one of a UDT's subtypes could be another UDT.

    A Class describes an object. A Class can be used in a similar way to a UDT but it can also do a lot more, eg have it's own methods properties etc. A Form is a class.

    Here is an example of how you could (but wouldn't) define a team as an object
    Code:
    Option Explicit
    
    Public Name As String
    Public Batting As Integer
    Public DefRating As Integer
    Public Position As Integer
    
    Private cThrowArm As Integer
    
    Public Property Get ThrowArm() As Integer
        ThrowArm = cThrowArm
    End Property
    
    Public Property Let ThrowArm(value As Integer)
        If value > 80 Then
            value = 80
        ElseIf value < 10 Then
            value = 10
        End If
        cThrowArm = value
    End Property
    
    Private Sub Class_Initialize()
        Debug.Print "I'm alive!"
    End Sub
    
    Private Sub Class_Terminate()
        Debug.Print "Bye!"
    End Sub
    ...and how you might then use it...
    Code:
    Option Explicit
    Private mTeams() As cTeam
    
    Private Sub Form_Load()
        ReDim mTeams(3)
        Set mTeams(0) = New cTeam 'creates a new instance of the class
        
        With mTeams(0)
           .Name = "thing"
           .DefRating = 32
           .ThrowArm = 99
        End With
        
        Set mTeams(1) = mTeams(0) 'creates a second reference the 1st team
        
        Debug.Print mTeams(1).DefRating
        Debug.Print mTeams(1).ThrowArm
    
        set mteams(1) = nothing 'destroys said reference
    End Sub
    Note how Throwarm has some restrictions as to what values can be set to it.

    Classes are a lot slower than UDT's but much more powerful.
    Last edited by Milk; Feb 13th, 2009 at 07:50 AM.

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