【发布时间】: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#