could you help me?

Post your coding questions such as 'how to' here.
Post Reply
lanslotfrog
Newcomer
Posts: 11
Joined: Wed Mar 12, 2014 8:43 am

could you help me?

Post 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
lanslotfrog
Newcomer
Posts: 11
Joined: Wed Mar 12, 2014 8:43 am

Re: could you help me?

Post 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
Sly_Jack0
Devoted Member
Posts: 612
Joined: Mon Dec 10, 2007 8:25 am

Re: could you help me?

Post by Sly_Jack0 »

You need to declare the variable as global for it to be accessible inside a function:

Code: Select all

Global theRect.RECT = New(RECT)
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.
Post Reply