Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Thebardstale · Jan 30, 2014 at 01:48 PM · class objectassign-variableclass instance

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KellyThomas · Jan 30, 2014 at 01:51 PM 3
Share

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>
avatar image MrVerdoux · Jan 30, 2014 at 03:41 PM 0
Share

@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!

avatar image Thebardstale · Jan 30, 2014 at 04:03 PM 0
Share

The Dictionary sounds like what I need, thanks a lot, I'll try that.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges