1

I have a struct that looks like this:

public struct ChannelSettings
{
    public double slider20Hz;
    public double slider25Hz;
    public double slider31_5Hz;
    public double slider40Hz;
}

and I am looping through my user interface objects when a slider value is changed like this:

foreach (Slider slider in grdEqSliders.Children)
{
     // slidername = slider.Name;
     switch(currentChannel)
     {
         case 1:
            // Assign to channel.slidername
            break;
         case 2:
            // Assign to channel.slidername
            break;
         default:
            break;
      }
}

My question is, is there a way for me to get the sliders name then turn it in to something I can reference directly in my structure? For example I would like to turn the slider.Name = "slider_20Hz" in to the variable slider20Hz that I use in my struct.

3
  • 1
    Well, you can switch on a string; just have the cases be each of those four strings, rather than 1, 2, 3, 4. You could use reflection, but in a case this simple the switch is probably easier. Commented May 6, 2013 at 19:26
  • I will have multiple structs though and need to make sure the value is getting put in to the correct one. Commented May 6, 2013 at 19:28
  • 1
    Accessing the appropriate variable (once you have its name) is addressed by this question here - you can do so using reflection. A (possibly) better solution, if it works for your codebase, would be store the doubles in a dictionary - the current var "name" becomes the key. Commented May 6, 2013 at 19:33

2 Answers 2

6

The only way to do that leaving the struct defined as it is now is to use reflection - pretty messy though.

An alternative - changing the definition - is to store the values in a dictionary:

public class ChannelSettings {
  Dictionary<string, double> _values = new Dictionary<string, double>();

   public double slider20Hz {
     get {
       return GetByName("slider20Hz");
     }
     set {
       _values["slider20Hz"] = value;
     }
   }

   . . . 

   public void SetByName(string name, double value) {
     _values[name] = value;
   }

   public double GetByName(string name) {
     double v;
     if (_values.TryGetValue("slider20Hz", out v)) {
       return v;
     }
     return 0.0; // Default value
   }

}

and then to set a value given its name you can write:

channel.SetByName(slider.Name, . . . )

and to get a value:

channel.GetByName(slider.Name)
Sign up to request clarification or add additional context in comments.

4 Comments

I hadn't thought about doing it that way, this should work perfectly. Thanks.
There is no way to do that leaving the struct defined as it is now. Sure there is; you can do exactly what you've done using reflection if you really want to, although you probably wouldn't want to.
How would I call this to get the information back out by passing in the string name?
@IanOswald: see updated answer (if I understood correctly your question)
1

The Slider control, as all FrameworkElements, has a Tag property for these types of situations where you need a property for your own use.

<Slider Tag="slider_20Hz"/>

foreach (Slider slider in grdEqSliders.Children)
{
     slidername = slider.Tag as string;
     ...
}

1 Comment

Yes, I am getting that, but then need to reference the variable in the struct with the similar name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.