Page 1 of 1
could you help me?
Posted: Mon Apr 28, 2014 7:31 am
by lanslotfrog
why i can't assign a custom type to global variable?
and,why i can't use custom type variable in function block?
such as:
Code: Select all
type RECT
field x
field y
field width
field height
end type
theRect.RECT = new(RECT)
function DoSomething()
theRect\x = 10
end function
as the code above,cannot complie,what should i do
Re: could you help me?
Posted: Mon Apr 28, 2014 7:36 am
by lanslotfrog
and,the custom type can be nested?
such as:
Code: Select all
type position
field x
field y
end type
type rect
field position
field width
field height
end type
Re: could you help me?
Posted: Mon Apr 28, 2014 8:35 am
by Sly_Jack0
You need to declare the variable as global for it to be accessible inside a function:
You can nest Types. Sort of at least. Basically, you can only have primitive types as fields so to nest types you have to use ConvertToInteger() and ConvertToType() functions to store and retrieve the nested type. So for example:
Code: Select all
'Type declarations
Type POSITION
Field x
Field y
EndType
Type RECT
Field position
Field width
Field height
EndType
'Create a new rectangle and set its width and height
r.RECT = New(RECT)
r\width = 10
r\height = 10
'Create a new position instance
tmpPosition.POSITION = New(POSITION)
tmpPosition\x = 100
tmpPosition\y = 200
'Use ConvertToInteger() to store position in the rectangle
r\position = ConvertToInteger(tmpPosition)
'To access rectangle's position you need to use ConvertToType()
position.POSITION = ConvertToType(r\position)
Print position\x
Print position\y
Hopefully my advice is correct. Haven't used CoolBasic since forever.