I have this method: Public Shared Function Test(ByVal T As ProjectStatus) And I call it this way: Test(ProjectStatus.Approved Or ProjectStatus.Archived) How do I access each one in the function? I debugged, and the value of the parameter when you pass that
in is "6", because Approved = 2 and Archived = 4.
When you call it that way, it performs a bitwise OR operation. The result is binary 110, which as you said evaluates to int 6. Why are you calling the method that way? I would suggest that you call the Test method, and then inside the method check to see whether
the ProjectStatus is Approved or Archived. If this isn't what you are looking for, post more code so we can understand what you are doing.
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
Sorry. Here's some more information: I have a database table which has a list of items. One of the columns is "Status", which is the Int value of the Enum. The Test() method selects the items from the database. I want to be able to select rows with two different
types of Status. Right now I'm just passing in an array of Enum values. This works, but is kind of annoying if you only want to pass in one Enum value. Also, the parameter is "Optional", which means I can't have two overloads: Test(Optional ByVal Status as
ProjectStatus()) Test(Optional ByVal Status as ProjectStatus)
Try using a ParamArray. ParamArray Optional. Used only as the last argument in arglist to indicate that the final argument is an optional array of elements of the specified type. The ParamArray keyword allows you to pass an arbitrary number of arguments to
the procedure. A ParamArray argument is always passed using ByVal. Find out more in the docs:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconUnderstandingParamArrays.asp
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
Great, except it doesn't work with method signatures with optional parameters. I'm going to split it up into different overloads and use the ParamArray. Thanks!
anyObject.GetType().InvokeMember("MethodName", BindingFlags.Default Or BindingFlags.InvokeMember, null, anyObject, new Object() {})
Isn't that method able to see which BindingFlags enum values you passed in? I've done a lot of my application using Optional Parameters, so I'd rather use them instead of overloads.
Woah, why are you using Reflection to call code? Are you creating code on the fly for some reason? I've messed around with CodeDOM, but not enough to help you out there. :) Param arrays work like this:
'Declare the method
Sub StudentScores(ByVal Name As String, ByVal ParamArray Scores() As String)
Dim I As Integer
Debug.WriteLine("Scores for " & Name & ":")
' Use UBound function to determine largest subscript of array.
For I = 0 To UBound(Scores)
Debug.WriteLine("Score " & I & ": " & Scores(I))
Next I
End Sub
'Call the method from another class
'Notice the 3 different ways to call the method
'You can keep adding as many scores as long as your code can handle it
StudentScores("Anne", "10", "26", "32", "15", "22", "24", "16")
StudentScores("Mary", "High", "Low", "Average", "High")
Dim JohnScores() As String = {"35", "Absent", "21", "30"}
StudentScores("John", JohnScores)
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
No I'm not using Reflection at all. It was just an example I found. ParamArray's work great, but I'd rather keep all my methods into one signature, just using the Optional keyword. I noticed how that paricular Reflection method used the OR keyword to specify
multiple BindingFlags. How did they do that, and how could I do the same thing?
Turn around and use the bitwise AND operator to see what flags were set.
Dim myStatus As ProjectStatus = ProjectStatus.Approved Or ProjectStatus.Archived
If myStatus And ProjectStatus.Approved Then
' Has Approved set
End If
' note: not an ELSEIF!
If myStatus And ProjectStatus.Archived Then
' Has Archived
End If
Vintious
Participant
1570 Points
314 Posts
Using muliple Enum's for a single parameter
Jul 13, 2004 03:42 PM|LINK
DarrellNorto...
All-Star
86645 Points
9629 Posts
Moderator
MVP
Re: Using muliple Enum's for a single parameter
Jul 13, 2004 04:45 PM|LINK
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Vintious
Participant
1570 Points
314 Posts
Re: Using muliple Enum's for a single parameter
Jul 13, 2004 04:52 PM|LINK
DarrellNorto...
All-Star
86645 Points
9629 Posts
Moderator
MVP
Re: Using muliple Enum's for a single parameter
Jul 13, 2004 07:55 PM|LINK
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Vintious
Participant
1570 Points
314 Posts
Re: Using muliple Enum's for a single parameter
Jul 14, 2004 03:39 PM|LINK
Vintious
Participant
1570 Points
314 Posts
Re: Using muliple Enum's for a single parameter
Jul 14, 2004 03:59 PM|LINK
anyObject.GetType().InvokeMember("MethodName", BindingFlags.Default Or BindingFlags.InvokeMember, null, anyObject, new Object() {})Isn't that method able to see which BindingFlags enum values you passed in? I've done a lot of my application using Optional Parameters, so I'd rather use them instead of overloads.DarrellNorto...
All-Star
86645 Points
9629 Posts
Moderator
MVP
Re: Using muliple Enum's for a single parameter
Jul 14, 2004 05:25 PM|LINK
'Declare the method Sub StudentScores(ByVal Name As String, ByVal ParamArray Scores() As String) Dim I As Integer Debug.WriteLine("Scores for " & Name & ":") ' Use UBound function to determine largest subscript of array. For I = 0 To UBound(Scores) Debug.WriteLine("Score " & I & ": " & Scores(I)) Next I End Sub 'Call the method from another class 'Notice the 3 different ways to call the method 'You can keep adding as many scores as long as your code can handle it StudentScores("Anne", "10", "26", "32", "15", "22", "24", "16") StudentScores("Mary", "High", "Low", "Average", "High") Dim JohnScores() As String = {"35", "Absent", "21", "30"} StudentScores("John", JohnScores)Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Vintious
Participant
1570 Points
314 Posts
Re: Using muliple Enum's for a single parameter
Jul 15, 2004 03:06 PM|LINK
jef06
Participant
895 Points
179 Posts
Re: Using muliple Enum's for a single parameter
Jul 15, 2004 04:56 PM|LINK
"Jef"
pickyh3d
Star
9696 Points
1955 Posts
Re: Using muliple Enum's for a single parameter
Jul 18, 2004 05:45 AM|LINK