dcsimg
Results 1 to 10 of 10

Thread: A Class within a Class?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    A Class within a Class?

    I have a Class module, let's call it ClassA, and in this Class I have a Public Function like this:


    Public Function SomeFunction(cA As ClassA)

    does the cA As ClassA put the Class itself within the Function?

    What is the purpose in doing this?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,999

    Re: A Class within a Class?

    No it does not

    In this case it passes the instance of the class to the function from the calling routine so you can work with items in that instance

    It is really pretty much the same as if you passed a textbox

    Code:
    Private SomeSub(txt as TextBox)
        debug.print txt.Text
    end sub
    This allows you to work with all the properties of the text box control that is passed in the parameter even if this code is in a module, the text box is private or whatever

    Passing an instance of a class works pretty much the same way
    Last edited by DataMiser; Nov 12th, 2013 at 02:37 PM.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: A Class within a Class?

    I can understand doing this in another module but not in the same module.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,621

    Re: A Class within a Class?

    Think of a tree structure.
    An object can have a parent and can have children, all of the same object type.
    Like the TreeView control.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: A Class within a Class?

    Quote Originally Posted by Arnoutdv View Post
    Think of a tree structure.
    An object can have a parent and can have children, all of the same object type.
    Yes, this is a rather frequent thing to do. The VB6 documentation covers this in the sections on Organizing Objects and Object Models.

    Similar things are often used when creating polymorphic classes and class factory objects.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: A Class within a Class?

    It isn't that I don't understand this object can have a parent and children of the same object. That makes sense to me.

    In module B I do this

    Dim cA As ClassA

    then in module B I do this

    cA.SomeFunction

    Above makes sense

    but this

    in module ClassA

    Call SomeFunction(cA As ClassA,.......)

    where SomeFunction is in ClassA

    Why does SomeFunction need to have cA As ClassA in it's argument list?

    It can reference any Sub, Function, Variable or Constant in that Class without prefixing cA to it. So I ask, what's the point.

    BTW: I'm not very good at understanding Classes and that's why I tend to avoid them.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,264

    Re: A Class within a Class?

    That is so that the function can operate on another instance of the class.

    So you could do something like this:
    Code:
    Dim A as ClassA
    Dim B as ClassA
      ...code to set A and B 
    
      X = A.SomeFunction(B)
    It is rare to have a reason to do that, but it does happen. The only one that is coming to mind at the moment is a kind of Clone function, which copies some of the properties from A to B.

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,999

    Re: A Class within a Class?

    Well first thing is that a Class is not a module, they are 2 very different things and behave very differently.

    Your question is a bit confusing since you are talking about modules and classes

    When you are using a class you create an instance of it in code, any data assigned to that class is only part of that instance. If you create another instance it can not see the data from the first.

    Like I said before it is not much different than say a textbox, a textbox is basically a class and each textbox on your form has its own instance of that class so when you assign .Text to text1 it has no effect on the text in text2 even though they are the same class behind the scenes.

    You would pass a class to a function as a parameter when you need to work with a specific instance of that class
    It would be unusual to pass an instance of a class to a procedure within that same class however and could possibly cause an issue depending on what the routine does and what the class is.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,719

    Re: A Class within a Class?

    It is far from a rare thing.

    You could have a Class that can contain a Collection of itself as child nodes (the tree case already mentioned in post #4 for example).

    This Class might have an AddChild() method that accepts a reference to another instance of the same Class to add to the Collection. It might just as easily have a CreateChild() method that initializes a new instance of the same Class, adds it to the Children Collection, and returns a reference to that new instance.

    The same class may even have serialization methods that walk through the object model to serialize and deserialize.


    There isn't a lot of point in trying to learn this by dealing with issues in isolation. Using the self-study sections of the VB6 documentation or taking a class is your best bet, though good VB6 classroom training is a rare thing to find these days. Avoid so-called tutorials lying around on the Web, and frankly most of the 3rd party VB6 books were always crap with very low information density.

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,423

    Re: A Class within a Class?

    JM, Another common scenario: As you know, an instance of a Class is a pointer, if you do this..
    Code:
    Dim oPerson1 as Person, oPerson2 as Person
    
       Set oPerson1 = New Person
    
       Set oPerson2 = oPerson1 
    .. you're not creating a 2nd instance of the class, you're making another variable point to the same object, so you need something else that will create a new instance with the same exact properties, this is when a Clone() property or function must be created and it's a good example to do what you've shown:
    Code:
        Set oPerson2 = oPerson1.Clone()
    ..And here it is, inside the Person Class:
    Code:
    Private mName     As String
    Private mAge      As Long
    
    Public Property Let Age(pNewVal As Long)
        mAge = pNewVal
    End Property
    
    Public Property Let Name(pNewVal As String)
        mName = pNewVal
    End Property
    
    Public Property Get Clone() As Person
    Dim oPerson As Person
    
        Set oPerson = New Person
        oPerson.Name = mName
        oPerson.Age = mAge
        
        Set Clone = oPerson
    End Property
    ..And the call from outside:
    Code:
        Dim p As oPerson, p2 As oPerson
        
        Set p = New oPerson
        
        p.Name = "Jhon"
        p.Age = 30
        
        Set p2 = p.Clone()
        
        MsgBox p2.Age & "," & p2.Name
    Last edited by jcis; Nov 14th, 2013 at 08:49 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
  •  



Featured


Click Here to Expand Forum to Full Width