Any way to shorten syntax when using class interface in C#?
Let's say I have created a Slider User Control which implements ISlider Interface. I have some methods like SetVa开发者_C百科lue below which should accept both ISlider types and standard Slider control type. Am I'm obliged to use this heavy syntax is there any shortcut ?
public void SetValue(Object slider, Double value) {
ISlider ISlider;
ISlider = slider as ISlider;
if (ISlider != null)
{
ISlider.Value = value;
}
else
{
((Slider)slider).Value = value;
}
As you can't make the wpf Slider
control inherit from your ISlider
, use an overloaded method.
public void SetValue(ISlider slider, double value)
{
slider.Value = value;
}
public void SetValue(Slider slider, double value)
{
slider.Value = value;
}
This isn't pretty, but saves you a headache as you don't have to check if the object is the correct type.
精彩评论