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
0
Question by roamcel · Oct 08, 2011 at 05:06 AM · classcustomenumdatatypepreset

Logic and datatype existential problem

The title might sound scary but I have just a question about data types.

Quite simply, I would need something like this:

 public class myclass {
   int myint = 0;
   string mystring = "";
 
   public myclass(int localint, string localstring) {
     myint = localint;
     mystring = localstring;
 
   }
 }
 
 public enum myenum : myclass {
   enum1 = new myclass(9,"abr"),
   enum2 = new myclass(99,"acad"),
   enum3 = new myclass(999,"abra")
 }

So that elsewhere, when I need 'abra', instead of manually instantiating it, and having countless duplicates all over the code, I just

 myenum mylocalenum;
 mylocalenum = enum3; //no mistake, the underlying class variables are predefined

The purpose is to have a selectable, pre-set 'myenum' which basically encapsulates another data structure which I predefine in the declaration phase.

This is because I have several data pre-sets by design, and I need to interact with them as with an enum (get their number, their descriptions, and basically associate them with predefined values).

If you have a solution, or even a resembling alternative, please let me know.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by kromenak · Oct 08, 2011 at 07:45 AM

Couldn't you use inheritance and polymorphism for this sort of thing? Unless I'm misunderstanding, you want to be able to assign a value to a variable that represents a class with different values. From your example above, each enum essentially just represents a pair of numbers. You could just have a base class with some abstract functions which return data in the child classes.

But I guess I could understand resisting that route - lots of overhead, extra files - no good. I guess you could do something like this:

 public class MyClass {
     ...
 }

 public enum MyEnum {
     class1 = 1,
     class2 = 2,
     class3 = 3
 }

 public class OtherClass {
     MyClass[] classes;

     public void Awake() {
         classes = new MyClass[3];
         classes[class1] = new MyClass(99, "asda");
         ....
     }

     public MyClass GetStuff(MyEnum type)
     {
         return classes[type];
     }
  }

But it still feels kind of messy. I'd probably still go with inheritance. You simply can't use an enum in the way you are describing.

Comment
Add comment · Show 1 · Share
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 roamcel · Oct 11, 2011 at 09:38 AM 0
Share

What an horrible shortco$$anonymous$$g for c#. looks like java can do this easily ins$$anonymous$$d. Being that we don't have typedefs even, what I need can't see$$anonymous$$gly be achieved in design time.

avatar image
1

Answer by kenruze · Oct 14, 2011 at 03:17 PM

Add a constructor to your class that takes that enum as the only parameter and then have the constructor switch on the type to set the values.

 public class myclass {
   int myint = 0;
   string mystring = "";
 
   public myclass(int localint, string localstring) {
     myint = localint;
     mystring = localstring;
   }

   public myclass(myenum id)
 {
     switch(id)
    {
      case enum1:
         myint = 9;
         mystring = "abr";
         break;
      case enum2:
         myint = 99;
         mystring = "acad";
         break;
      case enum3:
         myint = 999;
         mystring = "abra";
         break;
   }
 }
 
 public enum myenum : myclass {
   enum1,
   enum2,
   enum3
 }
Comment
Add comment · Show 1 · Share
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 roamcel · Oct 15, 2011 at 05:38 AM 0
Share

This is a good approach too, thanks a lot.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

PropertyDrawer: Enum to select extended class 0 Answers

Custom AudioReverbPreset 0 Answers

custom class serialization problem 1 Answer

Enum style variable 2 Answers

Unknown identifier: 'types'. js(38,61) 1 Answer


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