Author Topic: How to use enums?  (Read 312 times)

ashesman

  • Newbie
  • *
  • Posts: 12
    • View Profile
How to use enums?
« on: February 16, 2017, 03:24:35 pm »
The following code produces an error on the line " StreamDecodeState = SDS_WaitingStart", "Variable SDS_WaitingStart is undeclared.  How do I use enums?

Class NMEADecoder
   
    Private StreamData As ListOfByte
    Enum StreamDecodeState As Integer
        SDS_WaitingStart = 0
        SDS_WaitingEnd = 1
        SDS_WaitingCS = 2
    End Enum
   
    Private DecodeState As StreamDecodeState
   
    Public Sub New()
        StreamData = New ListOfByte
        DecodeState = SDS_WaitingStart
    End Sub
   
« Last Edit: February 16, 2017, 03:55:14 pm by ashesman »

Share on Facebook Share on Twitter


guest103

  • Guest
Re: How to use enums?
« Reply #1 on: February 17, 2017, 10:45:42 am »
The following code produces an error on the line " StreamDecodeState = SDS_WaitingStart", "Variable SDS_WaitingStart is undeclared.  How do I use enums?

Class NMEADecoder
   
    Private StreamData As ListOfByte
    Enum StreamDecodeState As Integer
        SDS_WaitingStart = 0
        SDS_WaitingEnd = 1
        SDS_WaitingCS = 2
    End Enum
   
    Private DecodeState As StreamDecodeState
   
    Public Sub New()
        StreamData = New ListOfByte
        DecodeState = SDS_WaitingStart
    End Sub

   

Hey there,

Unfotunately, the usage of enums usage of local enums is not supported in mcScript at this time.

I think that the existence of enums but lack of any support  the fact that you can declare them within a class but not use them is misleading. I've recorded this as an issue in our bug database, and we will let you know here when it's been resolved. As an alternative solution in the meantime, I would suggest using plain old int constants instead of an enum.

Let us know how things progress.

- Eric

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: How to use enums?
« Reply #2 on: February 17, 2017, 11:32:45 am »
I've used enums in many cases, lots of the libraries use them, but in all cases they have to be declared outside of a class.

Take a look at the MQTT, accelerator or Timer libraries to see how we are using them.
Like Like x 1 View List

guest103

  • Guest
Re: How to use enums?
« Reply #3 on: February 17, 2017, 11:38:05 am »
I've used enums in many cases, lots of the libraries use them, but in all cases they have to be declared outside of a class.

Take a look at the MQTT, accelerator or Timer libraries to see how we are using them.

Thanks for the correction. I've updated my post appropriately.

I tried this out real quick, and the following example compiles just fine.

Code: [Select]
Enum StreamDecodeState As Integer
    SDS_WaitingStart = 0
    SDS_WaitingEnd = 1
    SDS_WaitingCS = 2
End Enum

Class MyClass
    Public Sub New()
        Dim state As StreamDecodeState = StreamDecodeState.SDS_WaitingCS
    End Sub
End Class
Like Like x 1 View List