Class Problem

C

Chris

Hi

I have a number of arrays of classes with which i want to copy to data from
one element to another, but when i do the data in the first class remains
referenced to the data in the second.

Perhaps i can make more sense with this:

Class MyClass1
dim a as byte
dim b as byte
end class

sub main
dim a(50) as myclass1

dim f as integer

' fill it with stuff
for f=0 to 50
a(f).a=rnd(255)
a(f).b=rnd(255)
next

' ok here is where the problem is

dim t as myclass1
t=a(25)
' if i look at a(25).a it could be say 30 and t.a is also 30

t.a =50
' at this point t.a should be 50 and a.(25).a should remain at 30
' but im finding in my program that a.(25).a is changing to 50 as well,
' it is like the t=a(25) is only setting a pointer to the reference in the
'a' array
' and not copying the data from a(25) to t making t a separate instance.

end sub

If this is supposed to happen haw do i get the data out of the required
element of
the array and put it into a var and lose its reference.
 
P

Phill. W

Chris said:
i want to copy to data from one element to another, but when
i do the data in the first class remains referenced to the data in
the second.

The magic word here is "referenced".
Object variables contain references (dare I say Pointers?) to objects,
and /not/ the actual objects themselves. When you assign one object
variable to another, you are /only/ copying the reference to the object,
not the "target" object itself.
If this is supposed to happen

It is
haw do i get the data out of the required element of the array and
put it into a var and lose its reference.

Many objects implement a Clone() method that produces a /separate/
copy of an existing object and returns the [new] reference to this copy.

Public Function Clone() as MyClass1
Dim You as New MyClass1

You.a = Me.a
You.b = Me.b

Return You
End Function

This obviously gets more fiddly if some of your properties are
readonly - some extra [Friend] Constructors might prove useful.

HTH,
Phill W.
 
C

Chris

Are you saying that is i have a class with 100's of elements then i have to
do the You.a = Me.a for ever instance in the class and for each sub class
thnre in..

Ouch,

Phill. W said:
Chris said:
i want to copy to data from one element to another, but when
i do the data in the first class remains referenced to the data in
the second.

The magic word here is "referenced".
Object variables contain references (dare I say Pointers?) to objects,
and /not/ the actual objects themselves. When you assign one object
variable to another, you are /only/ copying the reference to the object,
not the "target" object itself.
If this is supposed to happen

It is
haw do i get the data out of the required element of the array and
put it into a var and lose its reference.

Many objects implement a Clone() method that produces a /separate/
copy of an existing object and returns the [new] reference to this copy.

Public Function Clone() as MyClass1
Dim You as New MyClass1

You.a = Me.a
You.b = Me.b

Return You
End Function

This obviously gets more fiddly if some of your properties are
readonly - some extra [Friend] Constructors might prove useful.

HTH,
Phill W.
 
C

Chris Dunaway

No need to do that. Just make sure that you instantiate every object
in the array by using the New keyword:

Class MyClass1
dim a as byte
dim b as byte
end class

sub main
dim a(50) as myclass1

dim f as integer

' fill it with stuff
for f=0 to 50
a(f) = New MyClass '<-------------New line here
a(f).a=rnd(255)
a(f).b=rnd(255)
next


This way, each array element points to its own reference.

Chris
 
P

Phill. W

Chris,

Yep! Only /you/ know what the innards of your class looks like,
so only you can write the code that creates a copy of one.

Regards,
Phill W.

Chris said:
Are you saying that is i have a class with 100's of elements then i have to
do the You.a = Me.a for ever instance in the class and for each sub class
thnre in..

Ouch,

Phill. W said:
Chris said:
i want to copy to data from one element to another, but when
i do the data in the first class remains referenced to the data in
the second.

The magic word here is "referenced".
Object variables contain references (dare I say Pointers?) to objects,
and /not/ the actual objects themselves. When you assign one object
variable to another, you are /only/ copying the reference to the object,
not the "target" object itself.
If this is supposed to happen

It is
haw do i get the data out of the required element of the array and
put it into a var and lose its reference.

Many objects implement a Clone() method that produces a /separate/
copy of an existing object and returns the [new] reference to this copy.

Public Function Clone() as MyClass1
Dim You as New MyClass1

You.a = Me.a
You.b = Me.b

Return You
End Function

This obviously gets more fiddly if some of your properties are
readonly - some extra [Friend] Constructors might prove useful.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top