Accessing a type within a type

BlitzMax Forums/BlitzMax Beginners Area/Accessing a type within a type

(tu) ENAY(Posted 2005) [#1]
What's going wrong here:-

Type moose
Field x:Int = 0
End Type

Local here:moose = New moose
here.x = 1

Type spoon
Field my:moose = New moose
my.x = 1 ' Error
End Type


I can't seem to access my moose inside a spoon.

HELP! :)


QuietBloke(Posted 2005) [#2]
errmm...

you are trying to write code within the type definition I would guess ( sorry.. dont have access to BMax at the Mo ).



if you replace

my.x = 1

with

method New()
my.x = 1
end method

then my.x should get set to 1 when the moose is created.


Dreamora(Posted 2005) [#3]
field my:moose = new moose is not allowed as well.
you need to declare this within the constructor (method new() )

field, global, const are only for declaration of data fields.


(tu) ENAY(Posted 2005) [#4]
God this is real tricky.
As another example I have this:-

Strict

Graphics 640,480

Type pages
	Field _x:Int = 0
	Field _y:Int = 0
End Type

Type book
	Field mypages:pages[50]
End Type


Global mybooks:book

Repeat
	Cls
	
	mybooks.mypages[0]._x = 1
	
	DrawText(mybooks.mypages[0]._x, 50, 50)
	
	Flip
	
	FlushMem
Until KeyDown(KEY_ESCAPE)


But I can't seem to get at the fields of the type within a type. I tried putting them inside Method New() but I got a different error. Blitz doesn't throw an error and merrily compiles but throws a wobbler later.

This is actually really quite confusing. :(


Dreamora(Posted 2005) [#5]
field, global, const in types are only for declaration, not for initialisation!
Type book
	Field mypages:pages[50]
End Type


mypages:pages[50] is an initialisation no deklaration

Type book
	Field mypages:pages[]

        method new()
           mypages = new pages[50]
        end method
End Type


now the full source with all bugs corrected ( where several others in as well )
Strict

Graphics 640,480

Type pages
	Field _x:Int
	Field _y:Int
End Type

Type book
	Field mypages:pages[]

        method new()
           mypages = new pages[50]
        end method
End Type


Global mybooks:book = new book

Repeat
	Cls
	
	mybooks.mypages[0]._x = 1
	
	DrawText(mybooks.mypages[0]._x, 50, 50)
	
	Flip
	
	FlushMem
Until KeyDown(KEY_ESCAPE)



FlameDuck(Posted 2005) [#6]
Type moose
  Field x:Int = 0
End Type

Type spoon
  Field my:moose
End Type

mySpoon:Spoon = new Spoon
mySpoon.my = new Moose
mySpoon.my.x = 1

As for the other example
Strict
Graphics 640,480
Type pages
  Field _x:Int = 0
  Field _y:Int = 0
End Type

Type book
  Field mypages:pages[50]
End Type

Global mybooks:book = new book

Repeat
  Cls
  mybooks.mypages[0] = new page
  mybooks.mypages[0]._x = 1

  DrawText(mybooks.mypages[0]._x, 50, 50)

  Flip
  FlushMem
Until KeyDown(KEY_ESCAPE)
Hope it helped.

This is actually really quite confusing. :(
Most (all?) of your "mistakes" seem to be linked to creational problems, that is you forget to create objects.

Also, traditionally class names are in singular, as it makes code more easily readable by humans. (The compiler ofcourse, couldn't care less).


(tu) ENAY(Posted 2005) [#7]
Ah. I'm more used to C++ OOP and I was under the assumption that Blitz was auto creating my objects. Didn't realise I had to hierarchically create my sub types manually, which it seems is my problem.

I'm guessing in the case of my example you would need to have a 50x for loop to create each page too?

> Hope it helped.

Yes, definitely. I don't know what's more annoying, not knowing how to do something or knowing how to do something and then not knowing the syntax and then randomly guessing the synaxt and not knowing what's going on.

Cheers Mikkel, that's a great help. :)


FlameDuck(Posted 2005) [#8]
I'm guessing in the case of my example you would need to have a 50x for loop to create each page too?
That depends on how you want to address the creation issue. You can choose to have "lazy" initialization, where objects are generated "on demand", or you can intialize all the objects up-front.

Lazy instantation or initialization sacrifices some performance in exchange for memory resources.