-1

I am going to convert some legacy VB codes to C#.

The original code is Me.variable1 = New Integer() {}.

I used some converting website and get the answer as this.variable1 == new int[]. However, the IDE gives error "Wrong number of indices inside []; expected 1". It seems I have to declare the size of array.

What size of the array the VB codes gives us? is it size 1 or no size?

Thanks!

DennisL
  • 293
  • 1
  • 3
  • 12

2 Answers2

2

this.variable1 = new int[]{} should work. It just creates an empty array.

Unlucky is correct, you won't be able to add anything to this array, but if you get your array from something else (maybe for instance a service or another call), you can assign that array to this array.

Here is a working example.

https://dotnetfiddle.net/1xY2XV

Brendan Long
  • 276
  • 1
  • 6
1

== is used here as comparison operator, not assignment operator. you have to use = for assignment. You have to specify the size of the array or else you have to initialize its elements. So you should use like this :

this.variable1 = new int[10];

Or like this :

this.variable1 = new int[] { 10, 12, 15, 3, 2, 4, 19 };

This (int[] variable1 = new int[] { };) is valid declaration of an empty array, but you can't assign anything to this array.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Thanks! Since the VB coding creates an empty array, can we add element to this empty array created by VB codes? – DennisL Jan 23 '17 at 02:20