chang an object's value using a pointer/handle
BlitzMax Forums/BlitzMax Programming/chang an object's value using a pointer/handle
| ||
I want to store the pointer (or handle) of an Object variable - so that I can later use that pointer/handle to alter the original object's value. Using HandleFromObject/HandleToObject allow you to access an object as such - but if I alter the object value, the change is not reflected back in the object variable you originally obtained the handle from. So, how then can I make the following work: SuperStrict 'original variable Local myObj:Object = "AA" 'handle Local myHandle:Int = HandleFromObject(myObj) 'copy of original object Local sameObj:Object = HandleToObject(myHandle) 'variables are identical If myObj<> sameObj Then DebugStop 'indirect alteration of myObj does not work like this: sameObj = "BB" 'myObj still prints original value (AA) in stead of New value (BB).. Print "" Print "myObj = "+String(myObj)+" (should be BB)" End Thanks in advance, Danny Last edited 2010 Last edited 2010 |
| ||
Imagine this scenario........ myObj:String = "AA" sameObject:String = myObj sameObject: = "BB" myObj is still "AA" thats why it doesnt work. the 'String' is an 'Object' Could you explain more why you need this arrangement? to help to get it working. Last edited 2010 |
| ||
Nononono don't use handles, they are old hat. Just do this. Type TString Field value:String End Type Local a:TString = New TString a.value = "Original" Local b:TString = a b.value = "Changed" Print a.value You don't need handles, just making one object variable equal another will make them refer to the same object. The exception is strings - so you need to wrap them up in another object. Last edited 2010 |
| ||
I think strings are always passed by-value not by-reference. So the following line 'copy of original object Local sameObj:Object = HandleToObject(myHandle) What you're doing is copying the value of the result of HandleToObject(myHandle) into sameObject. But they are still two distinct objects. Now when you do 'variables are identical If myObj<> sameObj Then DebugStop Blitzmax always compares String by-value and not by-reference. So it may seem that the are they same object but they are not. They are only the same value. Last edited 2010 |
| ||
Strings are represented internally in a funny way which I'm not going to pretend to understand. Basically, they act more like integers (you copy them with =) but you can store them in an object variable, but that's where the similarities end. They don't occupy a normal position in memory. Last edited 2010 |