2

this snippet of code

Tuple<int,double>[, ,] myArray = new Tuple<int, double> () [xsize, ysize, zsize];

returns this error

Cannot apply indexing with [] to an expression of type 'Tuple'

Where I'm using Tuple structure as defined here.

Thank you for your help and many thanks to this website authors, this site helps me a lot for my day to day work.

Community
  • 1
  • 1
Beer4All
  • 183
  • 1
  • 7

2 Answers2

11

I'm guessing that you want this:

Tuple<int,double>[, ,] myArray = new Tuple<int, double>[xsize, ysize, zsize];
                                                       ↑
                               note: removed the () ───┘
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
1

Creating an array is slightly different from creating any other object in that you don't specify an argument list for the constructor. Remove the () after the new Tuple<int, double> to fix your issue.

Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
  • Don't forget about object initializers. They make parens redundant for the default constructor too. :) – bzlm Aug 05 '10 at 11:49
  • That's only optional, you can either include them or not. Either way you won't get any compilation errors, so it's pure syntactic sugar, unlike the syntax for creating arrays which doesn't permit specifying an argument list (real, hard syntax). In any case I thought the OP should at least know *why* the parentheses were redundant, rather than just telling him *how* to solve his issue. Though it seems other SO-ers don't find the "why" to have any additional value. – Allon Guralnek Aug 05 '10 at 11:55
  • @Allon Guralnek: It's really unclear what the problem of the OP might be, we can only guess. The `()` are obvious, but not necessarily the actual problem. So IMO it's sane to offer *how* to fix this small issue, and if it solves the problem, later explain *why* it solves it. (Which may not even be necessary if the OP immediately knows the *why* once he/she sees what was wrong.) Apart from that - Lasse was simply faster than you :-) – dtb Aug 05 '10 at 12:07
  • @dtb, I don't agree that the issue was unclear. He gave a line where he was obviously trying to create an array, and had a slight syntax error. There is nothing unclear about it. Also, I don't mind if the fastest answer gets all the votes, what I'm worried about is how it affects the quality of answers on SO. Remember, it's the answers (and questions) that matter, not the votes. – Allon Guralnek Aug 05 '10 at 13:19
  • @Allon Technically, there is no constructor involved at all, is there? – bzlm Aug 05 '10 at 13:48
  • @bzlm: Well it's an object, *someone* needs to construct it. Though it may not be a constructor like we know it (similar to how a deserialized object is constructed, not via the constructor, but in a special way). – Allon Guralnek Aug 05 '10 at 13:54