Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

- - - - -

MazeGen.Room.rWall': cannot have instance field initializers in structs


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 foolios   Members   -  Reputation: 133

Like
0Likes
Like

Posted 05 August 2007 - 08:54 AM

I was trying to create and use a structure but I get the following error: MazeGen.Room.rWall': cannot have instance field initializers in structs I tried the structure within the class and outside it:
    public struct Room
    {
        string rWall = "|";
        string floorDwn = "_";
    }


I've seen structures set up without static fields in other peoples projects.
    public struct Wall
    {
        public bool up;
        public int room1, room2;
    }

Whats wrong with how I am using it?

Ad:

#2 Gyna   Members   -  Reputation: 145

Like
0Likes
Like

Posted 05 August 2007 - 09:22 AM

hey foolios!

first, in your struct

public struct Room
{
string rWall = "|";
string floorDwn = "_";
}

you need to define the fields as public to make them accessible from the outside.


second, you can't initialize the fields inside the structure definition, you need to create an instance of the structure and set the members there. for example to create a public array of 10 Room structures:

public Room rm[] = new Room[10];

then you can set the values as follows:

rm[0].rWall = "hello";
rm[0].floorDwn = "world";

hope that helps.

oh, and in this particular case i'd use char instead of string :)

#3 foolios   Members   -  Reputation: 133

Like
0Likes
Like

Posted 05 August 2007 - 09:53 AM

That makes sense, thank you. I will give it a go.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS