-
May 6th, 2014, 09:23 PM
#1
Thread Starter
Addicted Member
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:
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.
-
May 6th, 2014, 09:28 PM
#2
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())
-
May 7th, 2014, 07:57 AM
#3
Thread Starter
Addicted Member
Re: Most efficient way to do this? Call sub with paramarray being passed to it
Originally Posted by jmcilhinney
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:
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.
-
May 7th, 2014, 08:03 AM
#4
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.
-
May 7th, 2014, 08:15 AM
#5
Thread Starter
Addicted Member
Re: Most efficient way to do this? Call sub with paramarray being passed to it
Originally Posted by jmcilhinney
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:
Enum testenum
Tryone = 64
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?
-
May 7th, 2014, 08:31 AM
#6
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.
-
May 7th, 2014, 08:52 AM
#7
Thread Starter
Addicted Member
Re: Most efficient way to do this? Call sub with paramarray being passed to it
Originally Posted by jmcilhinney
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:
Dim allCheckBoxes = Me.Controls.OfType(Of CheckBox)()
Dim checkedBoxes = allCheckBoxes.Where(Function(cb) cb.Checked)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|