我正在编写一个类库供我们公司的一些工程软件使用。该库用于定义结构钢形状的属性。在我的每个类对象中,我都需要去指定的文件夹并查找一些xml数据。如何在C#中的一个类的多个实例之间共享一个值?
我怎么能建立一个共同的变量我可以设置库类外实例筛选之间共享(如下面的代码是可能的)
class Program
{
static void Main(string[] args)
{
string someCommonVarialble = @"c:\some\path\where\the\xmlData\are\stored";
// create some steel shapes
SteelBeamShape myBeam1 = new SteelBeamShape("W6x9");
SteelBeamShape myBeam2 = new SteelBeamShape("W10x22");
SteelPipeShape myPipe1 = new SteelPipeShape("10odx.375wall");
SteelPipeShape myPipe2 = new SteelPipeShape("24odx.750wall");
// do some work with objects here
}
}
public class SteelBeamShape
{
// constructor
public SteelBeamShape(string SteelBeamNominalValue)
{
// look up some properties base on nominal value in XML tables
this.xmlDataPath = someCommonVariable;
// do stuff ....
}
}
public class SteelPipeShape
{
// constructor
public SteelPipeShape(string SteelPipeNominalValue)
{
// look up some properties base on nominal value in XML tables
this.xmlDataPath = someCommonVariable;
// do stuff ....
}
}
}
谢谢史蒂夫,这对我有帮助。我不得不做一些关于静态变量的研究。我一直认为静态对于实例来说是静态的。我没有意识到可以像这样使用静态变量。在创建实例之前,我也采用了pln的加载表的建议。所以我在最后的解决方案中做了什么,是设置我的对象类,以便他们将继承一个共同的基类,正如你所建议的。但是我使静态成员成为一个DataSet,它被加载到我的主程序的初始化部分。那样我就不用每次都加载它了,因为我们建议使用 – 2014-10-20 17:23:30
看来你已经很好地理解了静态的概念。好工作 – Steve 2014-10-20 17:25:40