1

I declare an ArrayList using:

[System.Collections.ArrayList]$arr = @("foo", "bar", "some", "string")

..Then verify its type:

$arr.GetType()

But inserting a new value, using Insert() returns an error:

$arr.Insert("anotherstring")

Cannot find an overload for "Insert" and the argument count: "1".

What does this error message mean? What is the correct format when using this method?

Is there a better way to append items to the ArrayList?

0

3 Answers 3

1

I guess what you're looking for is to add an element.

$arr.Add("anotherstring")

The error you're getting with insert is because this method inserts an element in a given position of the array. It takes two arguments: position and element. Since you pass only one argument, it raises an error.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - $arr | Get-Member does not display the Add method. Do you know why?
Because you're piping array elements, not the array itself.
@LightningWar Get-Member -InputObject $arr would get you what you are looking for.
0

You have to define the index of the element you're inserting, in other words the place where it will be placed.

$arr.Insert(1,"anotherstring")

After that your array will be like this

@("foo", "anotherstring", "bar", "some", "string")

New element of the array has index of 1

Comments

0

You can see the arguments expected like this, running the method without parentheses:

[Collections.ArrayList]$arr = echo foo bar some string
$arr.Insert
OverloadDefinitions
-------------------
void Insert(int index, System.Object value)
void IList.Insert(int index, System.Object value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.