12

I want to do this:

Dim Numbers As Integer()() = {{1}, {2}, {3}, {4, 5, 6, 7}}

The IDE's underlining 4, 5, 6, 7 and saying Array initializer has 3 too many elements. What am I doing wrong?

1

1 Answer 1

19

The following should work:

Dim Numbers As Integer()() = {({1}), ({2}), ({3}), ({4, 5, 6, 7})}

As documents in Arrays in Visual Basic:

You can avoid an error when you supply nested array literals of different dimensions by enclosing the inner array literals in parentheses. The parentheses force the array literal expression to be evaluated, and the resulting values are used with the outer array literal

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

1 Comment

Ah, I was wondering why all I was missing were those parens. Thanks, it worked awesomely.

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.