Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
IAutoTransmission Bentley = new AutoTransmission(5,80,50);

I am creating one object called Bentley. Inside the AutoTransmission class i want to get the object name dynamically when we are calling the constructor.
How we can get the object name ??

Inside the constructor i want to get the object name.like this

console.writeline("YOur object name is {0}",objname); //this has to print "Bentley"
Posted
Updated 16-Apr-12 21:39pm
v2
Comments
Lakamraju Raghuram 17-Apr-12 3:38am    
You mean that you want to get the name 'Bentley' inside AutoTransmission(....) ??
samu4u 17-Apr-12 3:46am    
Yes

You can't - the "object name" that you are talking about is a variable which references an object, of type IAutoTransmission. The actual object does not have a human readable "name".

Think about it: if you wrote this code
C#
IAutoTransmission Bentley = new AutoTransmission(5,80,50);
IAutoTransmission Mine = Bently;
Which name would you expect it to return?
 
Share this answer
 
Comments
Lakamraju Raghuram 17-Apr-12 3:44am    
+5.
If provided a one suppose, it will only open a can of worms
samu4u 17-Apr-12 3:52am    
Sorry,
Think in this way, The AutoTransmission is possible for most of the car.So we cant able to predict which car is using autoTransmission.
The name of the car is actually the object name.So i want to get the name of object inside the same class itself.
OriginalGriff 17-Apr-12 4:01am    
As I said: "You can't".
What if the object is an array element? What would it's name be? What if it was a property of a class? Should it's name be the classname.propertyname? or the classinstancevariablename.propertyname?
Think of what you are trying to achieve, and allow for naming in that scheme.
samu4u 17-Apr-12 4:10am    
Thanks...
samu4u 17-Apr-12 4:13am    
My friend said it is possible in Python.Then Why not in C#??
Thats why??
Short answer: you cannot do that.
However, please see this thread[^] at 'The Competitors' site.
 
Share this answer
 
Comments
Lakamraju Raghuram 17-Apr-12 3:45am    
+5 for you too (for leading to COMPETITORS)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900