multiple conflicting enums in multiple UDTs

General FreeBASIC programming questions.
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

multiple conflicting enums in multiple UDTs

Postby hooshnik » Mar 14, 2018 20:59

I am trying to use multiple dimmed enums that begin with the same symbolname that are always inside UDTs. I am not sure what is the best way to do this. Both of the enums have "undefined" as their first symbolname (seems to be the problem) and I get:

Code: Select all

 enum_test.bas(39) error 254: Ambiguous symbol access, explicit scope resolution required, found 'undefined' in '.modifiers = undefined'


I think this has something to do with namespaces but I'm not sure how to do this exactly. Here is the code:

Code: Select all

enum dirTypeTable
    undefined
    dpi
    dynCon 
end enum

enum directiveModTable
    undefined
    none
    knockback
    command
end enum

'type directiveTableFormat
    'genericArg as string
    'dirType as dirTypeTable
    'modifiers as directiveModTable
'end type


'''''''''''''''''''''''''''''''''''''''''''''''''

type dynConTable
    modifiers as directiveModTable
end type

dim dynConSettings as dynConTable
'redim as directiveTableFormat dirList(1 to 1)

with dynConSettings
    .modifiers = undefined
   
end with


So Right now if you comment out the enum block dirTypeTable it compiles. But what I want to do is use those enums in both of those UDTs. What is the best programming practice for this?
fxm
Posts: 8347
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: multiple conflicting enums in multiple UDTs

Postby fxm » Mar 14, 2018 21:13

Documentation wrote:An Enum is a useful way of grouping together a set of related Constants. A symbol can be accessed like a constant, e.g: a = symbolname. But if the name clashes with another symbol, it must be resolved using typename.symbolname.

Therefore:

Code: Select all

.modifiers = directiveModTable.undefined
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

Re: multiple conflicting enums in multiple UDTs

Postby hooshnik » Mar 14, 2018 21:57

That works! The only thing is you have to remember the typename and variable name you made for it to access it later. I think I could get used to it. Working code:

Code: Select all

enum dirTypeTable
    undefined
    dpi
    dynCon 
end enum

enum directiveModTable
    undefined
    none
    knockback
    command
end enum

type directiveTableFormat
    genericArg as string
    dirType as dirTypeTable
    modifiers as directiveModTable
end type

'''''''''''''''''''''''''''''''''''''''''''''''''

type dynConTable
    modifiers as directiveModTable
end type

dim dynConSettings as dynConTable
redim as directiveTableFormat dirList(1 to 1)

''''''''''''''''''''''''''''''''''''''''''
with dynConSettings
    .modifiers = directiveModTable.undefined               
end with       
               
with dirList(1)
    .modifiers = directiveModTable.knockback
end with       
               
print dynConSettings.modifiers
print dirList(1).modifiers
fxm
Posts: 8347
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: multiple conflicting enums in multiple UDTs

Postby fxm » Mar 14, 2018 22:20

Another solution:

Code: Select all

enum
    undefined
end enum

enum dirTypeTable
    dpi = 1
    dynCon
end enum

enum directiveModTable
    none = 1
    knockback
    command
end enum

type directiveTableFormat
    genericArg as string
    dirType as dirTypeTable
    modifiers as directiveModTable
end type

'''''''''''''''''''''''''''''''''''''''''''''''''

type dynConTable
    modifiers as directiveModTable
end type

dim dynConSettings as dynConTable
redim as directiveTableFormat dirList(1 to 1)

''''''''''''''''''''''''''''''''''''''''''
with dynConSettings
    .modifiers = undefined               
end with       
               
with dirList(1)
    .modifiers = knockback
end with       
               
print dynConSettings.modifiers
print dirList(1).modifiers
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

Re: multiple conflicting enums in multiple UDTs

Postby hooshnik » Mar 14, 2018 23:08

Only thing is setting 'none = 0' is unsafe and you get no warning 2 things have the same value. It is hella convenient though...

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest