Results 1 to 15 of 15

Thread: [RESOLVED] [Enum].GetValues

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Resolved [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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [Enum].GetValues

    Assuming .NET 3.5, you can use LINQ:
    vb.net Code:
    1. ComboBox1.DataSource = [Enum].GetValues(GetType(IO.Ports.StopBits)).Skip(1)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [Enum].GetValues

    Here's a messy sort of "fix":
    vb.net Code:
    1. Dim b(xStopBits.Length-1) As IO.Ports.StopBits
    2. Array.Copy(xStopBits,b,b.Length)
    3. Dim l As New List(Of IO.Ports.StopBits)(b)
    4. l.RemoveAt(0)
    5. ComboBox1.DataSource = l.ToArray()

  4. #4

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [Enum].GetValues

    Quote Originally Posted by jmcilhinney View Post
    Assuming .NET 3.5, you can use LINQ:
    vb.net Code:
    1. 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'.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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()

  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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.

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [RESOLVED] [Enum].GetValues

    John just missed the OfType function:

    vb.net Code:
    1. [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.)

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [Enum].GetValues

    Quote Originally Posted by ForumAccount View Post
    John just missed the OfType function:

    vb.net Code:
    1. [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:
    1. [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]
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [Enum].GetValues

    Quote Originally Posted by dbasnett View Post
    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:
    vb.net Code:
    1. Dim sb As StopBits
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED] [Enum].GetValues

    Quote Originally Posted by dbasnett View Post
    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.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [Enum].GetValues

    Quote Originally Posted by dbasnett View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width