将对象投入班级

发布于 2025-01-29 07:12:28 字数 2312 浏览 3 评论 0原文

我正在尝试创建一些具有两个变量的类。一个变量之一是名称,另一个是值。对于每个类值,可以是不同类型的变量(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2025-02-05 07:12:28

我更喜欢为此使用界面,但是您可以做这样的事情:

// ...
foreach (var parameter in parameters)
{
    parameter.SetTo(param); // Call same interface
}
// ...

在每个具体类中:

public class Integer : ElementProperty
{
    public new int Value;
    public Integer(string Name, int Value)
    {
        this.Name = Name;
        this.Value = Value;
    }
    public void SetTo(Parameter p)
    {
         p.Set(this.Value); // calls correct overload
    }
}

在没有开关/情况下,它可以完整地工作,或者/否则链。

但是,请注意,使用这种模式的冲动可能是潜在的设计问题的提示。这就是有时将其视为“代码气味”的原因。

I prefer using Interfaces for this, but you could do something like this:

// ...
foreach (var parameter in parameters)
{
    parameter.SetTo(param); // Call same interface
}
// ...

In each of those concrete classes:

public class Integer : ElementProperty
{
    public new int Value;
    public Integer(string Name, int Value)
    {
        this.Name = Name;
        this.Value = Value;
    }
    public void SetTo(Parameter p)
    {
         p.Set(this.Value); // calls correct overload
    }
}

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文