Assignment of class instance

  • Thread starter Thread starter Zvi Danovich
  • Start date Start date
Z

Zvi Danovich

Hi all,
I am newby in VB.NET (but have good experience in C++), and hope that my
question is very simple for experienced VB user.

The problem is:

Suppose I have the

class A
Dim x as integer
Dim y as integer
end class

and iArrayList having many instances of A.

In my VB.NET code I want assign one of members of ArrayList to some new
instance of A:

Dim iA as new A
iA = iArrayList(5)

In this case I see, that the iA is the REFERENCE to iArrayList(5): if I
write
iA.x = 18
I see, that after this action iArrayList(5).x appears to be 18 too !

But my goal is assignment of the VALUE of iArrayList(5) to iA, NOT reference
(pointer) !
When I write
iA.x = iArrayList(5).y
iA.y = iArrayList(5).y
I get what I want, i.e. by this way I do get the value assignment, but this
is not a good solution.

I am sure, that there is a correct way in VB.NET to assign one instance to
another without the list of assignments of all class members.

Thank you for advance,

Zvi
 
This is what I do:
Create a "Clone" method in class A which returns a copy of itself.
iA = iArrayList(5).Clone()
-Praveen
 
And what about using "Structure" instead of class ? If I will declare A as
Structure instead of Class, will it work properly in described case ?
 
I guess it will, if functionality your class is just limited to store those
two values.
 
* "Zvi Danovich said:
I am newby in VB.NET (but have good experience in C++), and hope that my
question is very simple for experienced VB user.

The problem is:

Suppose I have the

class A
Dim x as integer
Dim y as integer
end class

and iArrayList having many instances of A.

In my VB.NET code I want assign one of members of ArrayList to some new
instance of A:

Dim iA as new A
iA = iArrayList(5)

The code aboce doesn't make sense. You are creating a new instance and
then assign a reference to another instance to the variable.
But my goal is assignment of the VALUE of iArrayList(5) to iA, NOT reference
(pointer) !
When I write
iA.x = iArrayList(5).y
iA.y = iArrayList(5).y
I get what I want, i.e. by this way I do get the value assignment, but this
is not a good solution.

Implement the 'ICloneable' interface.
 
Back
Top