Programatically determine variable name
-
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).
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?
-
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).
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?
The approach you are using is probably not ideal. The only thing I can think of is to eliminate the members and add an indexor. Ideally, however, each property should access an individual member. It is the most logical method.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway -
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).
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?
You can't get the variable name from the object, as you are not accessing the variable. You are just accessing an object that the variable happens to be referencing. There is no backwards reference from the object to the variable. If you want the DataItem object to know the name of the variable, you have to store the name in the object.
Despite everything, the person most likely to be fooling you next is yourself.