UDT Constructor with Param

New to FreeBASIC? Post your questions here.
Post Reply
codeWrapped
Posts: 8
Joined: Apr 20, 2024 0:14

UDT Constructor with Param

Post by codeWrapped »

I am not new to programming nor OOP concepts however I am new to Free Basic. After reviewing documentation and playing around a bit I discovered the following question escapes me.

In Free Basic how can I pass a param to constructor of a UDT?

The syntax for supplying a param with DIM statement is the issue?

Thanks
fxm
Moderator
Posts: 12160
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT Constructor with Param

Post by fxm »

Look at the CONSTRUCTOR manual page with the examples.
You can also consult the Programmer's Guide, first on the Constructors and Destructors (basics) page.
codeWrapped
Posts: 8
Joined: Apr 20, 2024 0:14

Re: UDT Constructor with Param

Post by codeWrapped »

OK,

I think I missed that page! I think that will help me out.

Thank you
codeWrapped
Posts: 8
Joined: Apr 20, 2024 0:14

Re: UDT Constructor with Param

Post by codeWrapped »

That page helped me fix my issue!

One other question I have about constructors.

What is the preferred method of handling constructors with lots of parameters? In JavaScript its painless to pass them as an object giving you named parameters that are also position less.

Someone in the Forums mentioned using Enums, but there is a limit of 30?


Thanks again fmx
fxm
Moderator
Posts: 12160
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT Constructor with Param

Post by fxm »

There is no size limitation for an 'Enum' structure in FreeBASIC.
Löwenherz
Posts: 48
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: UDT Constructor with Param

Post by Löwenherz »

Hello Made this little example with enums
Dont know If there is a Limit for enums with signed integers found Something with 2 to the Power of 31-1

Code: Select all

' enum expl freebasic
'
Enum MyEnum
    option1 = 1
    option2
    option3
    option4
    option5
    option6
    option7
    option8
    option9
    option10
'   ...
'    option2^31 - 1 ' max value for signed integer
'    = 2147483648
    __
    MAX_VALUE = __ -1
End Enum

Print "Option #1:", MyEnum.option1
Print "Option #2:", MyEnum.option2
Print "Option #3:", MyEnum.option3
Print "Option #4:", MyEnum.option4
Print "Option #5:", MyEnum.option5
Print "Option #6:", MyEnum.option6
Print "Option #7:", MyEnum.option7
Print "Option #8:", MyEnum.option8
Print "Option #9:", MyEnum.option9
Print "Option #10:", MyEnum.option10
Print "Max Value:", MyEnum.MAX_VALUE

' Define an enum named Colors
Enum Colors
    Red = 1
    Green
    Blue
    Cyan
End Enum

' Define an enum named Directions
Enum Directions
    North=10
    East
    South
    West
End Enum

    ' Print values from the Colors enum
    Print "The value of Red is:", Colors.Red
    Print "The value of Green is:", Colors.Green
    Print "The value of Blue is:", Colors.Blue
    Print "The value of Cyan is:", Colors.Cyan
    
    ' Print values from the Directions enum
    Print "The value of North is:", Directions.North
    Print "The value of East is:", Directions.East
    Print "The value of South is:", Directions.South
    Print "The value of West is:", Directions.West

Sleep()
fxm
Moderator
Posts: 12160
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT Constructor with Param

Post by fxm »

In an 'Enum' structure, symbol names are encoded using the signed 'Integer' data type.
So 2^32 or 2^64 different values are available for the 32-bit or 64-bit freeBASIC, respectively.
But since multiple symbol names in an 'Enum' structure can be associated with the same value, the number of symbol names in an 'Enum' structure is not limited by the number of different values (only limited by the compiler capacity).
The size of an 'Enum' instance will always be that of an 'Integer' (no matter how many defined symbols are just declarations for the compiler assignment).
fxm
Moderator
Posts: 12160
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT Constructor with Param

Post by fxm »

For more information on 'Enum' than on the ENUM page of the manual, see also the Constants and Enumerations page of the Programmer's Guide.
Post Reply