Question about Types and Fields
BlitzMax Forums/BlitzMax Beginners Area/Question about Types and Fields
| ||
I'm having trouble figuring out BMax Types...Type Car Field Color:String End Type Honda_1:Car = New Car Honda_1.Color = "Red" Honda_2:Car = New Car Honda_2 = Honda_1 Honda_1.Color = "Blue" Print Honda_1.Color Print Honda_2.Color The output is: Blue Blue Shouldn't Honda_2.Color have remained "Red". It's like Honda_2 is an "Instance" of Honda_1, rather than just a copy. |
| ||
The line: Honda_2 = Honda_1 does *NOT* copy the value "red" from car 1 to car 2. What it does it make the variable `Honda_2` point to the same car that the variable `Honda_1` points to. They are both looking at the same car. Whatever color you then set car 1 to, both variables will be looking at. You think you have two separate cars. You DID have two separate cars after the "Honda_2:Car=New Car" line, but then you made the POINTER TO the second car be the same as the POINTER TO the first car. Changing the pointer doesn't change the content of the fields, and it doesn't copy them. You can have more than one variable pointing to the same instance of the type. If you want to make the CONTENT of Car 2 be the same as the CONTENT of Car 1, you do: Honda_2.Color=Honda_1.Color |
| ||
It's like Honda_2 is an "Instance" of Honda_1, rather than just a copy. Yes. Objects are "Passed by reference" (think of it as two references, sharing the object in question between them).In order to "copy" or "clone" an object you could have a clone method, and use it like this: Type Car Field Color:String Method deepClone:Car() Local temp:Car = New car temp.Color = Color Return temp End Method End Type Honda_1:Car = New Car Honda_1.Color = "Red" Honda_2:Car = Honda_1.deepClone() Honda_1.Color = "Blue" Print Honda_1.Color Print Honda_2.Color |
| ||
Thanks, that makes sense guys. FlameDuck: is that the best way to do it? What if I had 50 different fields describing Honda_1, and I wanted to copy that entire object to Honda_2? It seems like there should be a quicker method for that. |
| ||
FlameDuck: is that the best way to do it? Well that (much like almost anything in regards to writting software) is mostly a matter of religion. :o> What if I had 50 different fields describing Honda_1, and I wanted to copy that entire object to Honda_2? Then at least with a Clone method, you would only have to do it once.If you do have that many however, changes are you need to rethink your design. :o> |
| ||
You could use an array to replace all your fields.Const CARCOLOR:Int = 1 Const CARTYPE:Int = 2 Const CARMASS:Int = 3 Type TCar Const NumberOfStringFields = 5 Field StringArray$[] Method New() StringArray = StringArray[..NumberOfStringFields] End Method Method Clone:TCar() temp:TCar = New TCar For i = 0 Until NumberOfStringFields temp.StringArray[i] = StringArray[i] Next Return temp End Method End Type car1:TCar = New TCar car1.StringArray[CARCOLOR] = "red" car1.StringArray[CARTYPE] = "Viper" car1.StringArray[CARMASS] = "1500" car2:TCar = car1.Clone() car2.StringArray[CARCOLOR] = "white" car2.StringArray[CARMASS] = "1250" For i = 0 Until TCar.NumberOfStringFields Print car1.StringArray[i] Next Print For i = 0 Until TCar.NumberOfStringFields Print car2.StringArray[i] Next Now, if you want to increase the number of fields, you only have to adjust the value of the Const-field "NumberOfStringFields". The Clone-method also uses this new value directly, so cloning an existing object isn't a big problem. The use of constants is only there so you can use a name for the index, rather than remembering which index stores a specific value. Of course, for integers, you can have a separate array that only uses integers. You would have to adjust the Clone method and the New method to process these separate arrays too. For adding new fields, just: - adjust the NumberOfStringFields value - add Constants at the top of your code, so you can access the indexes of the array by a name |
| ||
PowerPC603: That's pretty clever...I like that! Thanks for everyone's input on this. |