0

The program I'm writing has a number of structures that varies depending on a number of factors, the main point being that I am unable to predict how many of these structs there will be so the program creates them itself.

the structs look like this;

public struct boardState
    {
        public int structid;
        public char[] state;
    }

How can I get the program to name these structs without me explicitly giving it a name?

Ideally, there would be an int called structNum that keeps track of how many structs have been made and then any new structs would be named based on the value of structNum, but I'm not sure if this is something that can done, is there a way or can you suggest a better way?

Thank you.

1
  • Your understanding of value types is flawed. Commented Apr 30, 2012 at 4:18

2 Answers 2

1

If you don't know the exact number of values you're going to have you can use a List<boardState> which can hold an arbitrary long number of values. Naming things dynamically at run time isn't a feature of .NET. There isn't really a name after you compiled.

var theList = new List<boardState>();
// add
theList.Add(new new boardState());
// get one
int id = theList[0].structid;
// iterate
foreach(var s in theList)
    // do something
int cnt = theList.Count;
Sign up to request clarification or add additional context in comments.

1 Comment

That does seem like the best solution, but I'm unsure how to do this. I've tried like this; int x = loseList.Count; loseList.Add(x); loseList[x].state = sentstate; but thats not working, how do I go about doing this?
0

You can't do dynamic variable names at run-time without a lot of mucking around with reflection.

Simplest solution would be to keep the structs in a List or similar collection:

List<boardState> boardStates = new List<boardState>();

Comments

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.