tags:

views:

69

answers:

2

hi,

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.

+7  A: 

I'm guessing that you want this:

Tuple<int,double>[, ,] myArray = new Tuple<int, double>[xsize, ysize, zsize];
                                                       ↑
                               note: removed the () ───┘
Lasse V. Karlsen
+1  A: 

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
Don't forget about object initializers. They make parens redundant for the default constructor too. :)
bzlm
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
@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
@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
@Allon Technically, there is no constructor involved at all, is there?
bzlm
@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