【问题标题】:How to use same instance name with multiple classes如何在多个类中使用相同的实例名称
【发布时间】:2013-09-13 12:57:55
【问题描述】:

我是 c# 新手,我想我想这样做,但也许我不知道也不知道!

我有一个名为 SyncJob 的类。我希望能够创建一个实例来备份“我的文档”中的文件(只是一个示例)。然后我想创建另一个 SyncJob 实例来备份另一个文件夹中的文件。所以,换句话说,我可以在内存中拥有同一个类的多个实例。

我首先在我的代码中声明对象 var,以便它下面的所有方法都可以访问它。

我的问题是:虽然使用相同的实例名称会在内存中为对象创建一个新实例,但我该如何管理这些对象?意思是,如果我想设置其中一个属性,我该如何告诉编译器将更改应用到哪个实例?

正如我一开始所说,也许这是管理同一类的多个实例的错误方案......也许有更好的方法。

这是我的原型代码:

Form1.cs

namespace Class_Demo
{
    public partial class Form1 : Form
    {
        BMI patient; // public declarition
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreateInstance1_Click(object sender, EventArgs e)
        {
            patient = new BMI("Instance 1 Created", 11); // call overloaded with 2 arguments
            displayInstanceName(patient);
        }

        private void displayInstanceName(BMI patient)
        {
            MessageBox.Show("Instance:"+patient.getName()+"\nwith Age:"+patient.getAge());
        }

        private void btnCreateInstance2_Click(object sender, EventArgs e)
        {
            patient = new BMI("Instance 2 Created", 22); // call overloaded with 2 arguments
            displayInstanceName(patient);
        }

        private void btnSetNameToJohn_Click(object sender, EventArgs e)
        {
            // this is the issue: which instance is being set and how can I control that?
            // which instance of patient is being set?
            patient.setName("John");
        }

        private void btnDisplayNameJohn_Click(object sender, EventArgs e)
        {
            // this is another issue: which instance is being displayed and how can I control that?
            // which instance of patient is being displayed?
            displayInstanceName(patient);
        }
    }
}

类文件:

namespace Class_Demo
{
    class BMI
    {
        // Member variables
        public string _newName { get; set; }
        public int _newAge { get; set; }

        // Default Constructor
        public BMI() // default constructor name must be same as class name -- no void
        {
            _newName = "";
            _newAge = 0;
        }

        // Overload constructor
        public BMI(string name, int age)
        {
            _newName = name;
            _newAge = age;
        }

        //Accessor methods/functions
        public string getName()
        {
            return _newName;
        }
        public int getAge()
        {
            return _newAge;
        }
        public void setName(string name)
        {
            _newName = name;
        }

    }
}

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    您可以使用public List<BMI> PatientList { get; set; } 而不是BMI patient;

    如果您有一个patient,您不确定访问哪个项目以及何时分配它会替换之前的项目

        public List<BMI> PatientList { get; set; }
        public Form1()
        {
            InitializeComponent();
            PatientList = new List<BMI>();
        }
    

    使用BMI 列表,您可以添加如下项目

    PatientList.Add(new BMI("Instance 1 Created", 11));
    PatientList.Add(new BMI("Instance 2 Created", 22)); 
    

    如果需要设置实例1的名称,可以通过索引获取项目

    PatientList[0].setName("John");
    

    或者您可以通过PatientList 循环通过属性之一找到patient

    如果您需要显示“John”的patient 详细信息,请使用 LINQ

    displayInstanceName(PatientList.FirstOrDefault(p=>p.Name =="John")); 
    

    【讨论】:

    • 是的,我认为这是解决方案,因为我阅读了您的建议。
    • 行公开列表 PatientList { get;放; } 给了我错误不一致的可访问性:属性类型“System.Collections.Generic.List”比属性“Class_Demo.Form1.PatientList”更难访问——我不确定这意味着什么。跨度>
    • 您需要将 BMI 类创建为 public public class BMI
    • 成功了,谢谢。 PatientList.Add(new BMI("Instance 1 Created", 11));由于 msg 对象引用未设置为对象的实例而出错。 (很抱歉问题反馈,但我确定这是最后一个故障):-)
    • 知道了...非常感谢您的帮助...效果很好,现在我可以从同一个类创建多个实例并轻松管理它们!
    【解决方案2】:

    如果您需要管理实例集合,请使用List&lt;BMI&gt; 或类似名称。通用的 List&lt;T&gt; 类可以容纳(几乎)任何类型的对象,易于使用等。它也是您将多次使用的 .NET 工具包的重要组成部分。

    另外,考虑重写您的 BMI 类以更有效地使用属性:

    class BMI
    {
        public string NewName { get; set; }
        public int NewAge { get; protected set; }
    
        public BMI()
            : this("", 0)
        { }
    
        public BMI(string name, int age)
        {
            NewName = name;
            NewAge = age;
        }
    }
    

    访问器方法不是必需的,除非您需要它们与其他系统进行互操作。在属性本身的 getset 访问器上使用修饰符,您可以创建公共读取/私有写入属性等。

    【讨论】:

    • 谢谢...我是 C# 新手,所以我必须研究您建议中的每一行...再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多