|
-
Jan 21st, 2010, 10:09 AM
#1
[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.
-
Jan 21st, 2010, 10:11 AM
#2
Re: [Enum].GetValues
Assuming .NET 3.5, you can use LINQ:
vb.net Code:
ComboBox1.DataSource = [Enum].GetValues(GetType(IO.Ports.StopBits)).Skip(1)
-
Jan 21st, 2010, 10:14 AM
#3
Re: [Enum].GetValues
Here's a messy sort of "fix":
vb.net Code:
Dim b(xStopBits.Length-1) As IO.Ports.StopBits Array.Copy(xStopBits,b,b.Length) Dim l As New List(Of IO.Ports.StopBits)(b) l.RemoveAt(0) ComboBox1.DataSource = l.ToArray()
-
Jan 21st, 2010, 10:18 AM
#4
Re: [Enum].GetValues
 Originally Posted by jmcilhinney
Assuming .NET 3.5, you can use LINQ:
vb.net Code:
ComboBox1.DataSource = [Enum].GetValues(GetType(IO.Ports.StopBits)).Skip(1)
I am but it says
Error 1 'Skip' is not a member of 'System.Array'.
-
Jan 21st, 2010, 10:27 AM
#5
Re: [Enum].GetValues
This
Code:
Dim b As Array = [Enum].GetValues(GetType(IO.Ports.StopBits))
Dim xStopBits(b.Length - 1) As IO.Ports.StopBits
ends up with both arrays having the same length.
This works
Code:
Dim b As Array = [Enum].GetValues(GetType(IO.Ports.StopBits))
Dim xStopBits() As IO.Ports.StopBits
Array.Resize(xStopBits, b.Length - 1)
Array.Copy(b, 1, xStopBits, 0, xStopBits.Length)
-
Jan 21st, 2010, 10:46 AM
#6
Re: [Enum].GetValues
Skip(1) doesn't work unless you cast it as an array of IO.Ports.StopBits. But to databind to this complex type you must also convert it into a List(Of T)
Code:
Dim xStopBits = CType([Enum].GetValues(GetType(IO.Ports.StopBits)), IO.Ports.StopBits()).Skip(1)
ComboBox1.DataSource = xStopBits.ToList()
-
Jan 21st, 2010, 06:19 PM
#7
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.
-
Jan 21st, 2010, 06:28 PM
#8
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.)
-
Jan 21st, 2010, 06:35 PM
#9
Re: [RESOLVED] [Enum].GetValues
 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]
-
Jan 21st, 2010, 07:05 PM
#10
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.
-
Jan 21st, 2010, 07:16 PM
#11
Re: [RESOLVED] [Enum].GetValues
 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.
-
Jan 21st, 2010, 07:30 PM
#12
Re: [RESOLVED] [Enum].GetValues
When it comes to SerialPorts 1 stop bit would be a reasonable default.
Last edited by dbasnett; Jan 21st, 2010 at 07:33 PM.
-
Jan 21st, 2010, 07:44 PM
#13
Re: [RESOLVED] [Enum].GetValues
 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.
-
Jan 21st, 2010, 07:47 PM
#14
Re: [RESOLVED] [Enum].GetValues
 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.
-
Jan 21st, 2010, 07:53 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|