Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,685 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,254 people online right now. Registration is fast and FREE... Join Now!




Programatically determine variable name

 
Reply to this topicStart new topic

Programatically determine variable name, or what would be a better architecture?

redivider
post 28 May, 2008 - 12:51 PM
Post #1


New D.I.C Head

*
Joined: 28 May, 2008
Posts: 10

Is there a way I can determine the name of the variable that is being accessed.
In the following example, when I call Item22.Description (or any other property of BigClass), I'd like to be able to find variablename (Item22 in this case).

CODE

class BigClass
{
  public static Hashtable DescriptionsTable; //this would be a singleton
  DataItem Item01;  
  .... DataItem Item64;
}
class DataItem
{
  double _Value;
  string Description
  {  get { return BigClass.DescriptionsTable[variablename]; } }
}



If I don't have access to this name I figure I'll have to add a second property to BigClass for each Item (ex. Item22Description...), which seems like a waste. And I don't want to have the descriptions in each BigClass because they can get quite lengthy and there will be hundreds of BigClass instances at a time.

Any ideas?
If there's another option, how could I re-architect without wasting too much space?

User is offlineProfile CardPM

Go to the top of the page

djkitt
post 28 May, 2008 - 03:05 PM
Post #2


D.I.C Head

**
Joined: 22 May, 2008
Posts: 128



Thanked 13 times
My Contributions


QUOTE(redivider @ 28 May, 2008 - 03:51 PM) *

Is there a way I can determine the name of the variable that is being accessed.
If there's another option, how could I re-architect without wasting too much space?



You could add a 'Name' property to your class and set it when you create an instance.

CODE


DataItem Item01 = new DataItem();
Item01.Name = "Item01";

class DataItem
{
  double _Value;
  string _name;

string Name
{
   get {return _name;}
   set {_name = value;}
}
  string Description
  {  get { return BigClass.DescriptionsTable[_name]; } }
}



That is basically what is done with Controls.
User is offlineProfile CardPM

Go to the top of the page

redivider
post 28 May, 2008 - 03:08 PM
Post #3


New D.I.C Head

*
Joined: 28 May, 2008
Posts: 10

Thanks. I guess I'll have to do that.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:00AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month