Results 1 to 33 of 33

Thread: Question about classes in VB6

Hybrid View

  1. #1
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,674

    Re: Question about classes in VB6

    Quote Originally Posted by Elroy View Post
    @couttsj & Eduardo: Variables are common to all Aliases of a class, but they're not at all common to all Instances of a class. Those two concepts are entirely different. Couttsj, I suspect you were aliasing your class rather than creating different instances when you were doing your testing.
    Sorry, but I don't understand the concept of aliasing. Is this aliasing?
    Code:
        ReDim mServer(MaxClients)
        For lNum = 0 To MaxClients
            Set mServer(lNum).Callback(lNum) = Me
            mServer(lNum).IPvFlg = 4
        Next
    And if so, how would it be done differently?

    J.A. Coutts

  2. #2
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,229

    Re: Question about classes in VB6

    Quote Originally Posted by couttsj View Post
    Sorry, but I don't understand the concept of aliasing. Is this aliasing?
    Yes

    Quote Originally Posted by couttsj View Post
    And if so, how would it be done differently?
    Code:
        ReDim mServer(MaxClients)
        For lNum = 0 To MaxClients
            Set mServer(lNum).Callback(lNum) = New Class1 ' New Instance with separate local variables.
            mServer(lNum).IPvFlg = 4
        Next

  3. #3
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: Question about classes in VB6

    Quote Originally Posted by couttsj View Post
    Sorry, but I don't understand the concept of aliasing. Is this aliasing?
    Code:
        ReDim mServer(MaxClients)
        For lNum = 0 To MaxClients
            Set mServer(lNum).Callback(lNum) = Me
            mServer(lNum).IPvFlg = 4
        Next
    The above would only work, in case you have defined the mServer()-Array with the New-keyword.
    If that is the case, then no - this is not "aliasing" (which is just another word for: "two different Variables point to the same thing").

    What I find surprising is, that not many devs have done a few tests themselves (a decade or two ago),
    by using a simple Class1 - and a few Debug-Statements in its Class-Init and Terminate-Events.

    Because doing so would shed a lot of light on "when things start to come online" and "when things are going to die"...

    Here's some code, which shows what's going on behind the scenes of your example:

    Class1:
    Code:
    Option Explicit
    
    Private Sub Class_Initialize()
      Debug.Print "Class1 instantiated: " & ObjPtr(Me)
    End Sub
    
    Public Property Get SomeProp() As String
      SomeProp = "Class1 PropAccess: " & ObjPtr(Me)
    End Property
    
    Private Sub Class_Terminate()
      Debug.Print "Class1 terminated: " & ObjPtr(Me)
    End Sub
    Form-Code:
    Code:
    Option Explicit
    
    Private mServer() As New Class1, lNum As Long
    
    Private Sub Form_Click()
        ReDim mServer(2) 'a Redim like that (without Preserve) destroys any potentially existing intances in the Array
        For lNum = 0 To UBound(mServer)
          Debug.Print mServer(lNum).SomeProp
        Next
    End Sub
    Please click repeatedly on the Form - whilst having a breakpoint at the first statement in Form_Click.
    Then step through it and watch the Debug-Window (there's only 3 entries in the Array, won't take long).

    HTH

    Olaf

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