Results 1 to 7 of 7

Thread: Most efficient way to do this? Call sub with paramarray being passed to it

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Most efficient way to do this? Call sub with paramarray being passed to it

    Hi,

    I am trying to figure out the best way to do this. As of now all my solutions include a long list of If statements but I feel like there is a much simpler way to do it.


    I have a sub called like so

    VB.net Code:
    1. Sub Act( x as integer, Paramarray test() as testenum)

    The values going into the param array are based on checkboxes on the form. e.g if checked means an enum from testenum is being passed in the paramarray.
    There are a total of 8-10 check boxes, so say 3 are clicked. What would be to shortest way to call the Sub and pass 3 of those enums through.

    I feel like this could be as simple as a single line but nothing is coming to mind. I haven't used a paramarray much either.

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

    Re: Most efficient way to do this? Call sub with paramarray being passed to it

    Assign either a string or numeric representation of the appropriate enum value to the Tag of each CheckBox. You can then use some LINQ something like this:
    Code:
    Dim allCheckBoxes = Me.Controls.OfType(Of CheckBox)()
    Dim checkedBoxes = allCheckBoxes.Where(Function(cb) cb.Checked)
    Dim selectedEnums = checkedBoxes.Select(Function(cb) CType(cb.Tag, TestEnum)
    
    Act(x, selectedEnums.ToArray())
    You can collapse all that down to a single line if you don't find it confusing:
    Code:
    Act(x,
        Me.Controls.OfType(Of CheckBox)().
                    Where(Function(cb) cb.Checked).
                    Select(Function(cb) CType(cb.Tag, TestEnum).
                    ToArray())

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Most efficient way to do this? Call sub with paramarray being passed to it

    Quote Originally Posted by jmcilhinney View Post
    Assign either a string or numeric representation of the appropriate enum value to the Tag of each CheckBox. You can then use some LINQ something like this:
    Code:
    Dim allCheckBoxes = Me.Controls.OfType(Of CheckBox)()
    Dim checkedBoxes = allCheckBoxes.Where(Function(cb) cb.Checked)
    Dim selectedEnums = checkedBoxes.Select(Function(cb) CType(cb.Tag, TestEnum)
    
    Act(x, selectedEnums.ToArray())
    You can collapse all that down to a single line if you don't find it confusing:
    Code:
    Act(x,
        Me.Controls.OfType(Of CheckBox)().
                    Where(Function(cb) cb.Checked).
                    Select(Function(cb) CType(cb.Tag, TestEnum).
                    ToArray())
    That is very impressive, I need to get more familiar with LINQ

    I seem to be getting a cast exception saying unable to convert from string to integer. It occurs here

    vb.net Code:
    1. Dim selectedEnums = checkedBoxes.Select[B](Function(cb) CType(cb.Tag, TestEnum)[/B]

    It doesnt make sense to me why it would be converting to integer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Most efficient way to do this? Call sub with paramarray being passed to it

    It's converting to Integers because the underlying type of all Enums is a whole number type and almost always Integer.

    What did you put in the Tags? The code I provided will convert Integers to enumerated values. If you want to put use Strings then you'll have to provide a conversion from String to the appropriate Enum.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Most efficient way to do this? Call sub with paramarray being passed to it

    Quote Originally Posted by jmcilhinney View Post
    It's converting to Integers because the underlying type of all Enums is a whole number type and almost always Integer.

    What did you put in the Tags? The code I provided will convert Integers to enumerated values. If you want to put use Strings then you'll have to provide a conversion from String to the appropriate Enum.
    Ahh makes sense.

    My enums are defined as so:

    vb.net Code:
    1. Enum testenum
    2. Tryone = 64
    3. Trytwo = 128

    Ideally I would like to use strings for the tags. Would you be able to point me in the right direction? The conversion I assume would be done with linq?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Most efficient way to do this? Call sub with paramarray being passed to it

    The conversion wouldn't be done with LINQ. You've already got the LINQ code. It's the lambda that's passed to the Select call that does the conversion. As you can see, the code I posted performs a conversion with CType, which will work from Integer to an Enum but not from String. Check out the documentation for the Enum class to learn how to convert from a String to an Enum.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Most efficient way to do this? Call sub with paramarray being passed to it

    Quote Originally Posted by jmcilhinney View Post
    The conversion wouldn't be done with LINQ. You've already got the LINQ code. It's the lambda that's passed to the Select call that does the conversion. As you can see, the code I posted performs a conversion with CType, which will work from Integer to an Enum but not from String. Check out the documentation for the Enum class to learn how to convert from a String to an Enum.
    I tried using enum.parse on the string from the tag and it works perfectly.

    I love how it went from needing to use multiple if statements to 4 lines

    I want to push it in to 1 line like your example but feel that is not as straight forward to understand for someone else reading it

    vb.net Code:
    1. Dim allCheckBoxes = Me.Controls.OfType(Of CheckBox)()
    2.  Dim checkedBoxes = allCheckBoxes.Where(Function(cb) cb.Checked)
    3.  Dim selectedEnums = checkedBoxes.Select(Function(cb) CType([Enum].Parse(GetType(testenum), cb.Tag), testenum))

    Thanks again for all the help.

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