Enum question

Discuss the Firewing language

Enum question

Postby Coccoliso » Thu Oct 02, 2014 3:31 pm

Hello,
there is a more "elegant" ( 1 row of code ) to get the enum value from a knowed ENUM better than reported in ToEnum function ?
thanks.
Code: Select all
Module VisConsts

Public enum VisEnum
    Reset          = 0
    Chiamate       = 1
    Attesa         = 2
    Connessione    = 3
    Mappa          = 4
end enum

public function ToEnum(bVis as byte) as VisEnum
   select bVis
      case 0  : ToEnum = VisEnum.Reset         : exit function
      case 1  : ToEnum = VisEnum.Chiamate      : exit function
      case 2  : ToEnum = VisEnum.Attesa        : exit function
      case 3  : ToEnum = VisEnum.Connessione   : exit function
      case 4  : ToEnum = VisEnum.Mappa         : exit function
   end select 
end function

end module
User avatar
Coccoliso
 
Posts: 178
Joined: Sat Sep 27, 2014 10:02 am

Re: Enum question

Postby Jerry Messina » Thu Oct 02, 2014 5:07 pm

It's not safe, but you could always just cast the byte...
Code: Select all
dim ve as VisEnum
dim bVis as byte

ve = VisEnum(bVis)

The thing is, that will let you put anything into variable 've', not just valid enum settings so it's not really a good idea.

You should probably have an 'else' clause in your ToEnum() function. What do you want to happen if bVis isn't a valid VisEnum item?
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Enum question

Postby Coccoliso » Thu Oct 02, 2014 6:00 pm

Yes Jerry, I ate the row ELSE ..
Nice system that you showed me.
I was expecting some conversion function, but in reality is the compiler that provides a specific function after an enum declaration.
Nice. I need to store the state in EEPROM, and reload at the power on : the user finds the last selected item in the previously and the ELSE case is not proposed.
User avatar
Coccoliso
 
Posts: 178
Joined: Sat Sep 27, 2014 10:02 am


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x