Object help

BlitzMax Forums/BlitzMax Beginners Area/Object help

JBR(Posted 2010) [#1]
Hi, I'm declaring a variable as v:Object, so that I can then do v:Object = New Mytype or v:Object = New AnyType.

To be able to use the v, I must use MyType(v).whatever = 100

Later in the program, v will be Nulled and asigned to another type.

Is there a way to avoid the cast as it no look pretty!

Type MyType
Field bigmap[1024*1024]
Field first
End Type

Type MyType2
Field bigmap[1024*1024]
Field second
End Type

GCCollect
Print GCMemAlloced()

Global a:Object=New MyType
GCCollect
Print GCMemAlloced()

MyType(a).first =200

a=Null

GCCollect
Print GCMemAlloced()

a:Object = New MyType2
MyType2(a).second = 100

GCCollect
Print GCMemAlloced()

a=Null
GCCollect
Print GCMemAlloced()


Jesse(Posted 2010) [#2]
no way around it!
Although, It can be assigned into a variable of type myType then do the work with the new variable:
local b:MyType = myType(a)
b.first = 200



JBR(Posted 2010) [#3]
Thanks Jesse, that's much better. Jim.


Czar Flavius(Posted 2010) [#4]
I'm not sure I understand what you are trying to do. As Jesse suggets, it's better to store it in a variable.

It would be better to have a base "MapType" with the common fields and methods, and then extend it for each type that you need. Then you can have a generic MapType variable that works like the Object variable except you can use the common fields and methods without casting.