- Home /
How to declare multiple variables using a for loop in class declaration?
I've searched the web for an answer to this question but I can't find anything, I'm not even sure it's possible to do in C#.
What I want to do is, at class declaration, use a for or a while (or something else that gets the same effect) to declare some variables. I want to do this so I don't have to keep track of the variable names as they are stored in Enum classes. So if I modify the Enum lists I don't have to go changing this other script accordingly.
I hope I'm being clear enough, I know this is a complicated thing. To give you an idea here's the code that I have (which doesn't work as monodevelop says I can't use a for in class struct or declaration).
[Serializable ()]
public class SaveData : ISerializable
{
public string profHeader;
public string playerName;
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
public string (AttributeName)cnt.ToString();
}
for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
public string (SkillName)cnt.ToString();
}
The idea is for me not to have to modify this script if I modify the Enums.
People with requirements like this write programs that generate source code(e.g. *.cs
files). Once you have written it to disk it can be compiled like any other. Unity allows for scripts to run in the editor so it is an ideal environment for this approach.
Another simpler approach is to use a:
Dictionary<AttributeName, string>
@Thebardstale, you should do as $$anonymous$$elly$$anonymous$$ says, there is no problem with creating a Dictionary
and filling it on the fly, also you can still acces each attribute by its name, so it´s very similar to having an instance. Vote him up!
The Dictionary sounds like what I need, thanks a lot, I'll try that.
Your answer
Follow this Question
Related Questions
Varying number of variables depending on power type? 0 Answers
C# finding gameobject that created this class, and do I need to clear them? 1 Answer
How do I check if a derived class is a certain class? 1 Answer
How do I check if a derived class is a certain class? 0 Answers
[CLOSED] How do I check if a derived class is a certain class? 0 Answers