Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mechlordx · Oct 26, 2015 at 03:57 PM · custom classtypecasting

Custom Class Type Casting (to string)

How can I format a typecasting function in my custom class so that when I pass it off as another parameter type, say string, it uses the function to return an instance of the new type? I'm guessing it is something like

static public string...something something?

Edit: The custom class is essentially a string holder, with a name and other flags

     public class Datum
     {
         public string name;
         public string value;
         public string type;

this has a few unnecessary variables omitted. It would be great if I could use simplified code to do things like the following: Datum datumExample = "null"; // Every variable is initialized to the string "null"

 // or
 void aFunction(Datum a = "null"){};
 aFunction();
 // or
 void aFunction(string a = "null"){};
 aFunction(datumExample); // Where datumExample automatically returns it's
 // .value variable
Comment
Add comment · Show 1
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 NewPath · Oct 26, 2015 at 09:29 PM 1
Share

You are talking about two separate things. In terms of initialization, the most obvious way is just initialize all your members in the constructor:

 public class Datum
 {
     public Datum(string init)
     {
         name = init;
         value = init;
     }
 
     public string name;
     public string value;
 }
 
 Datum d = new Datum("initialValue");
 
 

I would also avoid using a string called "null" since that seems counter-intuitive (by assigning the string value "null" the string itself is not null). I would simply either set it to actual null, or string.Empty is a safer bet to avoid null reference exceptions.

For the second part when you want your class to have a string representation, just override the ToString() method that is available on all objects and return your value member.

 class Datum()
 {
     public override string ToString()
     {
         return value;
     }
 }



1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Socapex · Oct 26, 2015 at 09:36 PM

If you do not inherit MonoBehavior, than you can add a simple default constructor, or parameter constructor:

 public class Datum
 {
     public string name;
     public string val;
     public string type;
 
     public Datum() {
         name = "null";
         val = "null";
         type = "null";
     }
 }

 //or
 public class Datum
 {
     public string name;
     public string val;
     public string type;
 
     public Datum(string blee) {
         name = blee;
         val = blee;
         type = blee;
     }
 }

Then you can do Datum blou = new Datum(); or Datum blaa = new Datum("null");

In this case:

 void aFunction(string a = "null"){};
  aFunction(datumExample); // Where datumExample automatically returns it's
  // .value variable

Why not aFunction(datumExample.value); if it is public, or a simple getter aFunction(datumExample.getValue()); if it is private?

KISS :) Hope this helps.

[edit] I agree with @NewPath that using "null" is a little strange...

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 Mechlordx · Oct 26, 2015 at 10:40 PM 0
Share

this includes the extra steps im trying to avoid

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

32 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 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

Force typcasting of member objects in a List? (Boo) 1 Answer

CustomAttribute on Class or Method ( EDIT: Button to call methods) 2 Answers

Help removing a Custom Class from a List in UnityScript 1 Answer

How can I get functionality similar to OnValidate for a custom class? 4 Answers

Please Help! :) Loading assetBundle Animations 4.0.0 -> 4.0.1 broke my scene 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