[RESOLVED] [Enum].GetValues
The following works as expected.
Code:
Dim xStopBits As Array = [Enum].GetValues(GetType(IO.Ports.StopBits))
ComboBox1.DataSource = xStopBits
However, the enumeration of StopBits includes a value(None) that can't be assigned to a SerialPorts StopBits property. If you try you get System.ArgumentOutOfRangeException.
My question is how can I remove xStopBits(0) before setting the DataSource?
To get around this I currently have a check in the ComboBox1_SelectedIndexChanged event.
Re: [RESOLVED] [Enum].GetValues
I already posted in another thread about the conversion between an Array and its actual type, and dbasnett said it didn't work.
Re: [RESOLVED] [Enum].GetValues
John just missed the OfType function:
vb.net Code:
[Enum].GetValues(GetType(IO.Ports.StopBits)).OfType(Of IO.Ports.StopBits)().Skip(1)
(Arrays only implement IEnumerable and not the generic version, thus you don't get all the fancy stuff until it is cast.)
Re: [RESOLVED] [Enum].GetValues
Quote:
Originally Posted by
ForumAccount
John just missed the OfType function:
vb.net Code:
[Enum].GetValues(GetType(IO.Ports.StopBits)).OfType(Of IO.Ports.StopBits)().Skip(1)
(Arrays only implement IEnumerable and not the generic version, thus you don't get all the fancy stuff until it is cast.)
Actually, OfType is intended to be used as a filter to extract items of one type from a list containing multiple types, so it would actually be more appropriate to call Cast:
vb.net Code:
[Enum].GetValues(GetType(IO.Ports.StopBits)).Cast(Of IO.Ports.StopBits)().Skip(1)
Also, we know that None is the first value, so Skip(1) works but, if you want to be more explicit, you might exclude None specifically, so its actual position doesn't matter:[Enum].GetValues(GetType(IO.Ports.StopBits)).Cast(Of IO.Ports.StopBits)().Where(Function(sb) sb <> IO.Ports.StopBits.None)[/HIGHLIGHT]
Re: [RESOLVED] [Enum].GetValues
Thank you all. I guess I should also thank the folks at Microsoft for including a value for StopBits that is not accepted by the SerialPort. ;)
Re: [RESOLVED] [Enum].GetValues
Quote:
Originally Posted by
dbasnett
Thank you all. I guess I should also thank the folks at Microsoft for including a value for StopBits that is not accepted by the SerialPort. ;)
You'll find that they tend to do that if there's no other reasonable default value. Remember that enumerations are just a fancy face on numbers. If you do this:then your variable will have the value 0 and, therefore, whatever enumeration constant corresponds to that. If they hadn't added the None value then 0 would have to correspond to one of the other values and if there's no reasonable default choice then it's not reasonable to simply pick one at random to be the default. People might end up using the wrong value and not know it because the code would still run. At least this way, if you use None by accident then the code will throw an exception and you'll know you have to fix it. It's not always about what's convenient for you, but what's best for everyone.
Re: [RESOLVED] [Enum].GetValues
When it comes to SerialPorts 1 stop bit would be a reasonable default.
Re: [RESOLVED] [Enum].GetValues
Quote:
Originally Posted by
dbasnett
When it comes to SerialPorts 1 stop bit would be a reasonable default.
I think you misunderstood what jmc tried to say. A variable declared as an enum type is basically just an integer and the default value for an integer is 0, unless you provide a value.
Code:
Dim sb As StopBits
'No value assigned to "sb" above so it gets the default value 0.
BTW, I don't understand in this particular case why you guys suggest using the Cast extension method instead of a regular CType call as I posted earlier.
Re: [RESOLVED] [Enum].GetValues
Quote:
Originally Posted by
dbasnett
When it comes to SerialPorts 1 stop bit would be a reasonable default.
OK, let's say that you have a StopBits variable in your app that has the value One. How do you know whether the user set that or whether it was simply the default? What if you need to prompt the user to set a value if they haven't already but you can't tell whether they haven't already because there's no way to distinguish an explicit One from a default One? It's quite justifiable to have an "empty" value for an enumeration, even if that value can't actually be used. It saves you having to use a nullable variable if you need to be able detect that no value has been set.
Re: [RESOLVED] [Enum].GetValues
They are consistent in that the first value for Parity and Handshake are None also, but those are accepted values for the SerialPort. All of the enumerated setting for Parity and Handshake are accepted, StopBits is the exception.
A standard serial ports settings, in my recent experience, would be 9600-8-N-1.