I have a method which I need to call which accepts a ParamArray
Method(ByVal ParamArray elements() As Object)
I need to pass it two known strings, and an unknown number of strings, based on a variable number of XmlNodes in an XmlDocument
i.e.
Method("Known String 1", "Known String 2", Node1.OuterXml, ..., NodeN.OuterXml)
How can I do this?
I have tried looping through the nodes and creating and passing:
List(Of String)
which results in elements()
containing "Known String 1", "Known String 2", System.Collections.Generic.List[System.String]
List(Of String).ToArray
which results in elements()
containing "Known String 1", "Known String 2", System.String[]
String()
which results in elements()
containing "Known String 1", "Known String 2", System.String[]
and
Collection
which results in elements()
containing "Known String 1", "Known String 2", Microsoft.VisualBasic.Collection
What I want, for an example of 3 nodes is elements()
to contain "Known String 1", "Known String 2", "<node 1>", "<node 2>", "<node 3>"
Is this possible?
The issue here is the nonintuitive leap between what you write and what the compiler does - mainly because the syntax is a little wonky.
Anyhow, notice that your method signature contains a SINGLE parameter: elements()
. The ()
means its an array.
The ParamArray
keyword allows you to "explode" the elements of an array and pass each array element as its own parameter to the method - something you already knew. But it also means that the compiler (or JIT, or whatever) recombines those parameters back into a single array.
You would have noticed this if you hadn't had the misfortune of defining elements
as Object
:
so TL;DR:
Either pass each array element individually - not an option in your case,
Or pass a single array in for the array-typed parameter.
Dim stuffToPassInToTheMethod As List(Of String) = New List(Of String) From {"Magic String One", "Magic String Two"}
' Some loops and xml nodes.. blah blah.. end up calling stuffToPassInToTheMethod.Add(node.Attributes("yadda").Value
Then finally just pass it to the method: crazyMethodNameWithThePOINTLESSParamArray(stuffToPassInToTheMethod.ToArray)