Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Sep 17, 2019 2:39 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Problems with Copying classese
PostPosted: Wed Dec 09, 2009 7:55 am 
Offline

Joined: Tue Sep 08, 2009 10:00 am
Posts: 51
Good morning,
I have a problem using classes, maybe it's just a silly one.
I have on class with like this

MyClass
Prop1 as string
Prop2 as string

I have defined a variable "a" as MyClass and "a" is global variable (declared in a module).

Now,on a form,I want to copy "a" to "b", being "b" defined as a MyClass property of the form.

My problem is that from now on "b" is forever linked to "a" and if I make a change in "a" this reflects to "b".

Is there is a way to detach the two variables? Maybe I am adopting the wrong strategy. Can anyone help me providing me with some examples on how do this?

BEst regards


Top
 Profile  
Reply with quote  
 Post subject: Re: Problems with Copying classese
PostPosted: Wed Dec 09, 2009 8:08 am 
Offline
User avatar

Joined: Wed Nov 15, 2006 3:50 pm
Posts: 2353
Location: England
When you set a = b, what is happening is that the variable pointed to by 'a' is the variable b.
This is called aliasing.

To create a copy of a variable, you need to create a method which literally duplicates either the entire blob of memory occupied by A into the one occupied by B (dangerous because you dont know the storage method)
Or a method which copies the individual variables from B to A

This method can be an overridden constructor, or it can be an assignment.

For the sake of sillyness,create a class called Dog
Give it properties DogName as string, DogAge as integer

Add a function called Duplicate, returning Dog
code:

dim x as new Dog
x.DogName = self.dogName
x.dogAge = self.DogAge
return x



Then add this code to your startup window, and see that the two Dog variables begin the same, but end up different

dim fido as new dog
fido.DogName = "My name is Fido"
fido.DogAge = 6

dim rex as dog
rex = fido.Duplicate


//at this point rex and fido LOOK the same, but are in fact different objects
msgbox "Fido name =" + fido.DogName
msgbox "Rex name =" + rex.DogName

//change one, and only one of them changes
rex.DogName = "Rex"

msgbox "Fido name =" + fido.DogName
msgbox "Rex name =" + rex.DogName


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group