Results 1 to 4 of 4

Thread: Class Referencing byVal not ByRef

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Question Class Referencing byVal not ByRef

    How do we stop a class referencing another class?
    i/e

    Code:
    Dim tmpClass() as clsOrigClass
    tmpClass = myClass  'Makes a Copy
    Any changes made with tmpClass also affects myClass.....

    So instead of a REF how do we copy byVal?
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Class Referencing byVal not ByRef

    Hi some1uk03,

    I'll apologize in advance for being pedantic, but I think the language surrounding this stuff is important. So, I'm confused by your use of the word "class". A class is a section of code that is used to instantiate objects. In your above question, I believe you mean "object" and not "class". And, just to further clarify, once we have instantiated some object from a class, that variable will be a handle to that object. I don't know of a case where you have a variable (or handle) that directly addresses a class.

    So, to further clarify, an actual object (at least in the COM architecture) needs at least one variable referencing it, or it will un-instantiate. However, we can easily have several variables referencing it. (This is a type of aliasing, but we're better off calling it multiple references.)

    So, in your example above, you have an array of objects. I assume, somewhere, you will instantiate actual objects into the elements of that array. And then, you decide to make a copy of the entire array (your second line of code). And yes, that second line of code will create secondary references to each of the objects in the array.

    Now, if you want a fresh instantiation of each of your objects in that tmpClass array, you will have to individually instantiate them with the New keyword (or possibly the CreateObject function). There are other ways, but they'd be even more involved.

    Bottom line, I know of no way, through array assignment, to just instantiate an entirely new set of objects from the classes-of-the-objects in an object array being copied.

    Maybe that'll help.
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Class Referencing byVal not ByRef

    You would need to write your own method within your class to clone itself to a new class
    Code:
    Public Function Clone() As myClass
        Set Clone = New MyClass
        ... set all Clone properties from this class' current properties, i.e., Clone.PropertyA = Me.PropertyA
    End Function
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Class Referencing byVal not ByRef

    Also, I'm noticing the title of your thread, "Class Referencing byVal not ByRef".

    It's important to realize that the variable referencing an instantiated object holds an address to that object first and foremost. When passing object variables, the ByRef and ByVal refers to that four-byte address more than the actual object. For instance, we might have the following code:

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim c As Collection
    
        Set c = New Collection
    
        ByRefPass c
        ByValPass c
    
    End Sub
    
    Private Sub ByRefPass(ByRef c As Collection)
        ' do stuff
    End Sub
    
    Private Sub ByValPass(ByVal c As Collection)
        ' do stuff
    End Sub
    
    

    In the ByRefPass, we are essentially passing the same variable. If we un-instantiate it (Set c = Nothing), we will have also un-instantiated the object in the Form_Load procedure.

    Now, in the ByValPass, we are creating a new variable, but not a new object. In other words, we are creating a second reference to the same object. And this is quite similar to what your array duplication code is doing. In that ByValPass code, we can destroy the reference to our object (Set c = Nothing), but the object will still stay instantiated because the Form_Load procedure still has a separate reference to it.

    Good Luck,
    Elroy

    EDIT1: And, I guess what's important to recognize is that, nowhere in that sample code did we create a second collection. We were always referencing the same collection.
    Last edited by Elroy; Mar 21st, 2019 at 08:45 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

Tags for this Thread

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