2

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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
ThommyB
  • 1,456
  • 16
  • 34
  • Does this answer your question? [Jagged PowerShell array is losing a dimension when only one element exists](https://stackoverflow.com/questions/1390782/jagged-powershell-array-is-losing-a-dimension-when-only-one-element-exists) – ggorlen Mar 21 '23 at 01:01
  • Related: [1](https://stackoverflow.com/questions/64763381/how-to-create-nested-array-and-to-add-array-elements-in-it), [2](https://stackoverflow.com/questions/1390782/jagged-powershell-array-is-losing-a-dimension-when-only-one-element-exists) – ggorlen Mar 21 '23 at 01:09

1 Answers1

4

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
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Oz Bar-Shalom
  • 1,747
  • 1
  • 18
  • 33