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.

3
  • 3
    When you say "not working", what do you mean? Commented Aug 5, 2010 at 11:45
  • 3
    @Lasse Having a smoke, surfing, gaming, chatting... Commented Aug 5, 2010 at 11:47
  • 1
    The Tuple defined in the post you included has three values (Tuple<T, U, W>) and you're only using two. I hope that's not the issue you're having, but I just wanted to point it out. Commented Aug 5, 2010 at 11:56

2 Answers 2

11

I'm guessing that you want this:

Tuple<int,double>[, ,] myArray = new Tuple<int, double>[xsize, ysize, zsize];
                                                       ↑
                               note: removed the () ───┘
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

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

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.