'Type' Question

BlitzMax Forums/BlitzMax Beginners Area/'Type' Question

Ant(Posted 2005) [#1]
Hi, if you have 2 fields in a type - say A and B, and the third field C is the result of adding A and B:

Type Test
Field A
Field B
Field C=A+B
End type

global example:test
example = new test

example.a=10
example.b=20

...why wont example.c be calculated from the values of example.a and example.b? If doesnt seem to be able to access them and instead of 30, example.c will equal 0.

Thanks for any help.


GameScrubs(Posted 2005) [#2]
because you delcared type c as a variable. It gets calculated when the object variable is first created

example = new test

If you want to add them you have to create a method

Type Test
Field A
Field B
Field C

Method Add()
C = A + B
EndMethod
EndType

global t:Test = new Test
t.A = 10
t.B = 10
t.Add()
Print t.C


taxlerendiosk(Posted 2005) [#3]
Because it doesn't store the actual rule "A+B", it simply calculates it once when a new instantiation of the type is created. If you want to do this make C a method:
Type Test
  Field A
  Field B
  Method C()
    Return A+B
  End Method
End Type

...and call example.c() to get the value.


Ant(Posted 2005) [#4]
ah....ok, starting to get my head around this now. Thanks very much for your time guys