Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this project which I am working on, and I need help on C# structs. I am using a Console application.

What I'm after is to create a struct that I can use in a array. What I have so far is:

    public struct array
            {
                public static int id;
                public static int x;
                public static int y;
            };
    public static  array[] test = new array[amount];

Then what I want to be able to do is set the variables like this.

test[i].id = 1;
test[i].x = 1;
test[i].y = 1;

However it isn't working. If anyone has any ideas it would be appreciated a lot.

Thank you Adam

share|improve this question
Fixed the stupid syntax error, sorry. That wasnt the problem – Adam Meadows yesterday
What error do you get? – SLaks yesterday
3  
-1 for asking a completely bogus question, posting made up fake code, and also for describing the problem as "it isn't working". Please do try harder to ask a real question (never ever post fake code) and describe your problem properly. – David Heffernan yesterday
It wasn't a bogus fake code. When I was writing it out I made a simple mistake. I also fixed it within the minute. – Adam Meadows yesterday
You've posted two completely different variants now. At least one of them is fake. Yes you made a simple mistake. The mistake was that you were writing the code rather than pasting it. That's how you ended up posting fake code. What's more you have still not improved on "it isn't working". You really do need to take more time and care when asking questions. – David Heffernan yesterday
show 4 more comments

closed as not a real question by David Heffernan, sehe, codesparkle, Lynn Crumbling, Ted Hopp 23 hours ago

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

3 Answers

up vote 3 down vote accepted

You need to make the fields non-static.
static fields are associated with the type; not each instance.

share|improve this answer
Thanks, thats working now! – Adam Meadows yesterday

Another way to solve this problem is this:

 array[] test = new array[amount];
        array temp = new array();

        temp.id = 1;
        temp.x = 1;
        temp.y = 1;

        test[i] = temp;

And remove the static.

Hope this will help you

share|improve this answer

you should use appropriate c# syntax:

test[i].id = 1;
test[i].x = 1;
test[i].y = 1;
share|improve this answer
That was just me making an error writing out the question, sorry guys – Adam Meadows yesterday

Not the answer you're looking for? Browse other questions tagged or ask your own question.