Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing object member variable values in .net using an enum
    text
    copied!<p>[EDITED to remove the implication that I needed to access .net properties (vice member variables)]</p> <p>I'm trying to create a design pattern using .net (WPF coded in C# to be specific) by which instances of a class (derived from a base class) have member variables that can be accessed by passing an enum value in a method call. For example <code>myObject.GetValueOf(dimensionFields.Latitude)</code> would return the "Latitude" value held by myObject. Obviously the purpose is to use <code>GetValueOf([some enum variable])</code> at runtime to access any of a number of field values defined by an enumeration. I also need fairly high performance as these values will be accessed when many (could easily be well over 100,000) objects are drawn to a display (via OpenGL).</p> <p>Because of the performance requirements, I'm reluctant to use reflection--is that a reasonable reluctance?</p> <p>I have a solution in mind, but my question is: "is their a better or easier way?"</p> <p>Here's my current solution: I'll create enumerations for each type of member variable I want to access (i.e., one for doubles, one for integers, one for strings, one for bools, etc). The base class for my objects will have dictionaries corresponding to each type of variable (e.g., <code>Dictionary&lt;FieldsForDoubles, double&gt; m_doubleDict</code>). A <code>GetValueOf([some enum value])</code> method in that base class will be overloaded to accept each type of enumeration and return the appropriate data type (e.g., <code>public double GetValueOf(FieldsForDoubles field)</code>). Derived classes can add actual properties for convenience (e.g., a <code>Latitude</code> property), but those properties will simply access the appropriate dictionary.</p> <p>Finally, as an excursion, I might make the dictionaries accessible by a string value rather than an enum value, have the <code>GetValueOf</code> method access the dictionaries by using <code>someEnumValue.ToString()</code> and provide a generic method (<code>GetVaueOf&lt;type&gt;(string variableName)</code>) to allow certain derived classes to hold variables NOT in the current enumerations but accessible with a very similar pattern (e.g., I can use <code>GetValueOf&lt;double&gt;([some variable name])</code> to access any variable that uses this pattern)</p> <p>Anyone see issues with this method (e.g., accessing fairly short dictionaries isn't THAT expense, right?)? Any other thoughts on how this might be done?</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload