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 kaiyum · Feb 16, 2014 at 08:09 AM · c#initializationconstructors

initialize with object name

Hi, I have a field in a class(named ngut) like these:

 public string gsm;
 

Now the value of this field must be initialized when creating an instance of the respecting class. I want to initialize gsm field with object's name automatically. Thats it, after creating an instance, gsm should contain "X"(where X is the name of instanced object) string value. I set up constructor for this. But,

I do not want this,

 ngut sample=new sample("sample");

I want to simply write,

 ngut sample=new sample();

And gsm field automatically populate with "sample" value. Can this be done anyhow?

Comment
Add comment · Show 5
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 whydoidoit · Feb 16, 2014 at 09:30 AM 0
Share

In what way does this object you are creating have a name? Do you mean the name of the type? Do you mean that sample is the name of a class derived from ngut?

avatar image kaiyum · Feb 16, 2014 at 04:46 PM 0
Share

@whydoidoit, Suppose the class is this:

 public class ngut {
         private $$anonymous$$eyCode kb;
         private $$anonymous$$eyCode joy;
         private string gTag;
         private string gLayer;
         private string $$anonymous$$name;
         public ngut()
         {
                  //initialize the field
         }
         public ngut($$anonymous$$eyCode kbArg,$$anonymous$$eyCode joyArg)
         {
          //another overloaded constructor.
         }
         public void a$$anonymous$$ethod(){}
         public bool another$$anonymous$$ethod(){return true;}
         .........................................
         .........................................
         .........................................
         
 }

Now I want to create an instance of this class, and name the object to "sample". So I do like,

 ngut sample = new ngut();

You see there is a field called "$$anonymous$$name". I want to do something on a constructor which will make the value of "$$anonymous$$name" equal to "sample".

Lets say I created another instance of this class. The name of object is "sampleTwo". So I will write by,

 ngut sampleTwo = new ngut();

You see I did not initialize $$anonymous$$name field. But I want to write something on constructor code block( yes within "{}" block, Not the "()" block as argument), something sothat I do not have to initialize within "()". Normally every time I create an object I have to write like,

     ngut sample = new ngut(string $$anonymous$$nameArg);
     ngut sampleTwo = new ngut(string $$anonymous$$nameArg);

This way user have to pass a string value on constructor, every time he/she has to initialize $$anonymous$$name. And Also he/she has to be careful sothat string value matched the object's name he/she created. This regulation is required for artist's sake. As we know for sure that object's name=$$anonymous$$name field, I wanted to automate this. I tried something like this:

 public class ngut {
             private $$anonymous$$eyCode kb;
             private $$anonymous$$eyCode joy;
             private string gTag;
             private string gLayer;
             private string $$anonymous$$name;
             public ngut()
             {
              //initialize the field
              $$anonymous$$name=this.name;
                      
             }
             public ngut($$anonymous$$eyCode kbArg,$$anonymous$$eyCode joyArg)
             {
              //another overloaded constructor.
             }
             public void a$$anonymous$$ethod(){}
             public bool another$$anonymous$$ethod(){return true;}
             .........................................
             .........................................
             .........................................
             
     }
 

But it didn't work. Basically when user will use ngut class, I want them to create instances of ngut class. The $$anonymous$$name field will be automatically initialized. Is this even possible?

avatar image kaiyum · Feb 16, 2014 at 04:49 PM 0
Share

I wanted this sothat users do not have to write code for initializing $$anonymous$$name field.

avatar image RudyTheDev · Feb 16, 2014 at 05:39 PM 0
Share

@kaiyum: The problem with this is best illustrating by the fact that you could easily do new ngut(); without actually declaring a variable. The instance of the class is created before its reference is set to the variable. The class constructor has no way of knowing what you are going to do with the instance. Besides, what's stopping anyone from reassigning it to different variable, making multiple references, using an array element, a derived class, etc.

There is may be a way to reliably use reflection to find out what variables in the assembly are declared with your class type and find your class's reference. But it still won't work in constructor, because the reference is not yet set to that variable there.

avatar image kaiyum · Feb 16, 2014 at 06:19 PM 0
Share

hmm, so users of class should be more careful while creating objects then.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Deon-Cadme · Feb 16, 2014 at 09:19 AM

Hi,

I would recommend that you read up on static class members. They can easily deal with your problem as they are variables and arrays etc that exist at the same time in all objects that contain the class :)

I would personally just create a "static int count" variable. Increase count with one every time an object is created with the class and add it to the name like: "sample0", "sample1" and "sample2" etc.

Just note that you cannot decrease count if you randomly destroy the scene objects because that will cause duplicate names to appear. Just increase and add... ints are pretty big so I doubt that you will reach the limit ;)

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

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

21 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to initialize lists with set number of null elements? 1 Answer

Formatting Data Classes 3 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