Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Basic Object assigning problem (Byval not working)

Dear All,

I have Obj1 and Obj2 as objects of class1. When I access these 2 objects independently it works fine but when I say set Obj1=Obj2 it gets assigned by REF. So when I can Obj1's property Obj2's property also gets set.

Now I dont want this so I have written an function which accepts parameters as BYVAL and returns the objects which I again assigns to Obj1. Eg.


Function AssignByVal (Byval vdata as Object) as Object
   set AssignByVal = vdata
end sub

But this not works. It only works when I assign byval using piece - by piece method.
0
Shaileng
Asked:
Shaileng
2 Solutions
 
DoknjasCommented:
When you set an object reference to another object, both references point to the same object.  If you want to return a copy, then you have to do that yourself by implementing your own copy constructors - serialization will simplify this.

Passing the original object ByVal just ensures that the original object reference will not be pointing elsewhere after the method returns to the calling method.
0
 
Éric MoreauSenior .Net ConsultantCommented:
You need to implement a Clone method. See an article I wrote on the subject: http://www.utmag.com/July2003/Page8.asp
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now