将对象投入班级
我正在尝试创建一些具有两个变量的类。一个变量之一是名称,另一个是值。对于每个类值,可以是不同类型的变量(int,double或string)。
我想将这些类的实例存储在列表中,因此我将这些课程放在抽象类中。
然后,在一个foreach循环中,我想使用这些实例的值,但我需要将它们投入其原始类型,以便param.set函数可以接受。
我的代码就是这样:
List<ElementProperty> parameters = new List<ElementProperty>();
//I add my parameters to the list.
parameters.Add(new ElementProperty.String("TestName", "TestVariable"));
parameters.Add(new ElementProperty.Integer("TestName", 10));
//I want to make this foreach loop shorter and more proper
foreach (var parameter in parameters)
{
Parameter param = el.LookupParameter(parameter.Name);
if (parameter is ElementProperty.Boolean)
{
param.Set(((ElementProperty.Boolean)parameter).Value);
//param.Set only accepts int double and string
}
else if (parameter is ElementProperty.Double)
{
param.Set(((ElementProperty.Double)parameter).Value);
}
else if (parameter is ElementProperty.Integer)
{
param.Set(((ElementProperty.Integer)parameter).Value);
}
else if (parameter is ElementProperty.String)
{
param.Set(((ElementProperty.String)parameter).Value);
}
}
public abstract class ElementProperty
{
public string Name;
public object Value;
public class Integer : ElementProperty
{
public new int Value;
public Integer(string Name, int Value)
{
this.Name = Name;
this.Value = Value;
}
}
public class Double : ElementProperty
{
public new double Value;
public Double(string Name, double Value)
{
this.Name = Name;
this.Value = Value;
}
}
public class String : ElementProperty
{
public new string Value;
public String(string Name, string Value)
{
this.Name = Name;
this.Value = Value;
}
}
public class Boolean : ElementProperty
{
public new int Value;
public Boolean(string Name, bool Value)
{
this.Name = Name;
if (Value is false)
{
this.Value = 0;
}
else
{
this.Value = 1;
}
}
}
}
有更好的选择吗?任何建议都会有所帮助。 谢谢。
I am trying to create some classes with two variables. One of the variables is Name the other one is Value. For each class value can be different type of variables (int , double or string).
I want to store instances of these classes in a List so I placed the classes under an abstract class.
Then inside a foreach loop I want to use the Value of these instances but I need them casted into their original type so that the param.Set function will accept it.
My code is like this:
List<ElementProperty> parameters = new List<ElementProperty>();
//I add my parameters to the list.
parameters.Add(new ElementProperty.String("TestName", "TestVariable"));
parameters.Add(new ElementProperty.Integer("TestName", 10));
//I want to make this foreach loop shorter and more proper
foreach (var parameter in parameters)
{
Parameter param = el.LookupParameter(parameter.Name);
if (parameter is ElementProperty.Boolean)
{
param.Set(((ElementProperty.Boolean)parameter).Value);
//param.Set only accepts int double and string
}
else if (parameter is ElementProperty.Double)
{
param.Set(((ElementProperty.Double)parameter).Value);
}
else if (parameter is ElementProperty.Integer)
{
param.Set(((ElementProperty.Integer)parameter).Value);
}
else if (parameter is ElementProperty.String)
{
param.Set(((ElementProperty.String)parameter).Value);
}
}
public abstract class ElementProperty
{
public string Name;
public object Value;
public class Integer : ElementProperty
{
public new int Value;
public Integer(string Name, int Value)
{
this.Name = Name;
this.Value = Value;
}
}
public class Double : ElementProperty
{
public new double Value;
public Double(string Name, double Value)
{
this.Name = Name;
this.Value = Value;
}
}
public class String : ElementProperty
{
public new string Value;
public String(string Name, string Value)
{
this.Name = Name;
this.Value = Value;
}
}
public class Boolean : ElementProperty
{
public new int Value;
public Boolean(string Name, bool Value)
{
this.Name = Name;
if (Value is false)
{
this.Value = 0;
}
else
{
this.Value = 1;
}
}
}
}
Is there a better option? Any suggestion would help a lot.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更喜欢为此使用界面,但是您可以做这样的事情:
在每个具体类中:
在没有开关/情况下,它可以完整地工作,或者/否则链。
但是,请注意,使用这种模式的冲动可能是潜在的设计问题的提示。这就是有时将其视为“代码气味”的原因。
I prefer using Interfaces for this, but you could do something like this:
In each of those concrete classes:
This works completey without switch/case or if/else chains.
However, be aware that the urge to use this pattern may be a hint to underlying design issues. Which is the reason it is sometimes perceived as a "code smell".