ThommyB
ThommyB

Reputation: 1566

Multidimensional arrays with only ONE entry

I wrote a generic function to call a stored procedure. I tried to use a multidimensional array to pass the parameters. Now it is possible, that the procedure only takes one parameter, so my multidimensional array has also only one parameter. But the length of such an array is 2!

$MyParameters = ("param1_name", "param1_value")

$MyParameters.Length returns 2!! Strange, why? It should return 1

$MyParameters returns correctly:

param1_name
param1_value

If I write:

$MyParameters = ("param1_name", "param1_value"), ("param2_name", "param2_value")

$MyParameters.Length returns also 2 which is correct. $MyParameters returns correctly all four elements:

param1_name
param1_value
param2_name
param2_value

Any reasons for that? Am I missing something?

Upvotes: 2

Views: 1305

Answers (1)

Oz Bar-Shalom
Oz Bar-Shalom

Reputation: 1855

What you are trying to do is creating an array of multi value object.

Here is an example to solve that issue:

$x = ,("param1","param2","param3")
x.Length

Will return 1 which is correct for your issue.

$x = ,("param1","param2","param3"),("param1","param2","param3")
x.Length

Will return 2

$x = ,("param1","param2","param3"),("param1","param2","param3")
x[0].Length

will return 1, thats because $x[0] is array with one element.

In addition, If you would like to create an Array of Arrays this is the way to do it:

$x = @("param1","param2","param3"),@("param1","param2","param3")
$x.Length
#2
$x[0].Length
#3

Upvotes: 4

Related Questions