0

Is there a Visual Basic.NET method that can convert method parameters into an array?

For instance instead of:

Function functionName(param1 as object, param2 as object) as object
ArrayName = {param1, param2}

you could do something like:

Function functionName(param1 as object, param2 as object) as object
ArrayName = MethodThatGetsAllFunctionParams

Just curious really.

burntsugar
  • 57,360
  • 21
  • 58
  • 81
  • What exactly are you trying to achieve? There's not really enough detail in your question for me to tell. What does it mean to convert them into an array? – Cody Gray - on strike Apr 17 '11 at 15:44
  • Take a look @ http://stackoverflow.com/questions/3288597/is-there-a-way-to-get-an-array-of-the-arguments-passed-to-a-method – Alex K. Apr 17 '11 at 16:14

2 Answers2

3

Take a look at ParamArrays. I think this solves what you're asking for?

http://msdn.microsoft.com/en-us/library/538f81ec%28v=VS.100%29.aspx

EDIT:

You could initialise a custom collection using your current function signature

Public Class CustomCollection(Of T)
    Inherits System.Collections.Generic.List(Of T)

    Sub New(param1 As T, param2 As T)
        MyBase.New()
        MyBase.Add(param1)
        MyBase.Add(param2)
    End Sub
End Class

and then call the function using

Dim result = functionName(New CustomCollection(Of Object)(param1, param2))

The Function signature would be changed to:

Public Function functionName(ByVal args As CustomCollection(Of Object)) As String
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Smudge202
  • 4,689
  • 2
  • 26
  • 44
3

There is no way of doing that. The language itself doesn’t allow that. You can use reflection to get the currently executing method of the StackFrame of the execution. But even then it’s still impossible to retrieve the parameter values.

The only solution is to “patch” the applications by introducing point cuts into the method call. The linked answer mentions a possibility for that.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214