【问题标题】:How to make a string different for each instance of a class如何为类的每个实例制作不同的字符串
【发布时间】:2017-01-12 20:37:49
【问题描述】:

所以,我有一个名为 Condensed 的公共 partial 类。每个 condensed 实例都应该有自己的文件路径,它将从中加载数据,我试图通过一个名为 Path 的私有 static 字符串来创建该路径,然后创建 6 个 Condensed 实例,但我发现当我更改Path 的值之一时,Condensed 的所有实例都将它们的Path 值设置为最新的

public partial class Condensed : UserControl
{
    private static string Path;

    public Sender Export()
    {
           //this uses Path to Load data then return it to the main class
    }

    public void Load(string path)
    {
          Path = path
    }
}

然后在我的主类中,我执行以下操作:

public class Main
{
    public void Load_Condensed()
    {
         condensed1.Load(Paths[0]);
         condensed2.Load(Paths[1]);
         condensed3.Load(Paths[2]);
         condensed4.Load(Paths[3]);
         condensed5.Load(Paths[4]);
         condensed6.Load(Paths[5]);
    }

    private void exportToolStripMenuItem_Click(object sender, EventArgs e)
    {

        List<Condensed> Pokemon = new List<Condensed>
        {
            condensed1.Export(),
            condensed2.Export(),
            condensed3.Export(),
            condensed4.Export(),   //Loads all of the data from calling Export()
            condensed5.Export(),
            condensed6.Export()
        };

        Export(Condensed);  //Sends the data
    }
}

Paths[] 只是存储为公共字符串数组的文件路径数组。

基本上我希望每个 Condensed 实例都有自己唯一的字符串路径,可以在创建它的实例内部使用,我该怎么做?

【问题讨论】:

  • 取出字段中的static关键字:
  • 了解什么是静态的以及何时使用它。
  • @stuartd 我已经尝试过了,它不起作用:L
  • 为什么说没有静态就不行?删除 static 会发生什么?

标签: c#


【解决方案1】:

您的Path-字段是static,这意味着它在所有实例之间共享。 将其修改为实例字段,以使其对每个实例都不同:

public partial class Condensed : UserControl
{
    private string Path; // No static here

    public Sender Export()
    {
           this uses Path to Load data then return it to the main class
    }

    public void Load(string path)
    {
          Path = path
    }
}

事实上,它是一个部分类,实际上与它没有任何关系。部分类只是意味着您可以在单独的文件中定义类(在这种情况下,因为它是一个需要为 UI 设置一些代码的用户控件)

【讨论】:

  • 当我稍后调用路径时,路径显示为空
  • 你从哪里调用它?如果你这样使用它,它不应该是null
  • 看到你的更新。如果在调用 export 时 Pathnull,则可能意味着两件事:要么将 null 值分配到在 Load 方法中设置它和调用 Export 方法之间的某个位置,要么你正在传入null 值开头。我们需要更多代码才能看到发生了什么。
  • 如果您尝试在代码中从Condensed 类的外部 访问此字段,那么您可以考虑将访问修饰符更改为public
  • 最接近正确答案,我必须添加一些验证,但这有效:)
【解决方案2】:

static 引起了您的问题,因为它在 Condensed 类的所有实例之间使用 shared 内存,只需从您的属性中删除 static 关键字定义。

【讨论】:

    【解决方案3】:
    using System;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main()
            {
                var condensed1 = new Condensed() { FilePath = "/filepath1/" };
                var condensed2 = new Condensed() { FilePath = "/filepath2/blah/" };
                var condensed3 = new Condensed() { FilePath = "/filepath3/blah/blah/" };
    
                Console.WriteLine(condensed1.FilePath);
                Console.WriteLine(condensed2.FilePath);
                Console.WriteLine(condensed3.FilePath);
    
                Console.ReadKey();
            }
        }
    
        public partial class Condensed
        {
            public string FilePath { get; set; }
        }
    }
    

    【讨论】:

      【解决方案4】:

      添加新的构造函数,如

        public Condensed(string path)
      {
            Path = path
      }
      

      当你创建一个 Condensed 类的实例时,像这样调用它

      Condensed c1 = new Condensed("C/Desktop/blabla");
      c1.Export();
      

      【讨论】:

      • 这没有帮助。他的领域是静态的,所以不会有什么不同
      • 我在设计器中创建了压缩类(它们是我在另一个表单中使用的表单)所以我不能这样做,因为我从不在我的主代码中“创建”一个 Condensed 实例
      猜你喜欢
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 2017-04-02
      • 1970-01-01
      • 2017-07-15
      • 2011-08-27
      • 1970-01-01
      相关资源
      最近更新 更多