Enum Question

08-02-2007, 01:47 AM
Dear all,

I not sure whether I'm doing it the wrong way. I am looking for suggestion to make it improve it.

Let say I am creating an Enum that required floating point values,
e.g. the stopbit for serial port communication.

public Enum StopBit {
Stop1 = 1, Stop15 = 1.5, Stop2 = 2

This will give me an error.
Are there any workaround for this?

Thanks and Best Regards,

08-02-2007, 01:56 AM
Use constants.

08-02-2007, 07:54 AM
If you had tried it by actually declaring the underlying type, you would have seen the error:
Enums must be declared as 'Byte', 'Short', 'Integer', or 'Long'.

I believe this probably has to do with the fact that it is practically impossible to guarantee that two Double values will be considered equal by the equality operator but I could be wrong.

Just use constants, like Rockoon said.

08-02-2007, 04:22 PM
You could use constants or keep your Enum (ranging from 0 to whatever) and use it as an index into a pre-initialized array.

08-02-2007, 07:48 PM
Dear all,

Thanks for the prompt reply. ;)
I think I will consider using constant. Thanks.
But its interesting to note that the SerialPort control provided, does allow us to select One, OnePointFive, and Two, maybe they are using an index.

To be very frank, I am actually using C#, and I would like to seek some reference to convert my exisiting vb6 DCB structure to C#.

Best Regards,

08-02-2007, 10:16 PM
The StopBits enum:
Public Enum StopBits
' Fields
None = 0
One = 1
OnePointFive = 3
Two = 2
End Enum

I followed the chain of calls starting from the SerialPort constructor that seemed to use it, but it gets passed to some native method somewhere. Odds are they have a Select statement that just looks at the value.

It's interesting you mention DCB because that is the name of the structure where my search stopped. It seems it's an internal nested class of Microsoft.Win32.UnsafeNativeMethods.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum