2014-10-18 154 views
0

我正在编写一个类库供我们公司的一些工程软件使用。该库用于定义结构钢形状的属性。在我的每个类对象中,我都需要去指定的文件夹并查找一些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 .... 
    } 
} 

}

回答

0

要在不同的类之间共享一个共同的值,您需要从共同祖先派生的类。你的情况,你可以定义

public class SteelShape 
{ 
    public static string commonPath {get;set;} 
    ... add other common functionality .... 
    ... for example, how to load the xml file .... 
    public static XDocument LoadData(string fileName) 
    { 
     ...... 
    } 
} 

那么你的类从这个基类

public class SteelPipeShape : SteelShape 
{ 
    .... 
} 

得到时,你需要使用祖先类指的是你提到它通过你的类的实例共享的公共变量命名

SteelShape.commonPath; 

所以把所有在一起,你有

void Main() 
{ 
    // Set the path once and for all 
    SteelShape.commonPath = @"d:\temp"; 

    SteelBeamShape myBeam1 = new SteelBeamShape("W6x9"); 
    SteelBeamShape myBeam2 = new SteelBeamShape("W10x22"); 
    SteelPipeShape myPipe1 = new SteelPipeShape("10odx.375wall"); 
    SteelPipeShape myPipe2 = new SteelPipeShape("24odx.750wall"); 
} 

public abstract class SteelShape 
{ 
    public static string commonPath {get;set;} 
    public static XDocument LoadData(string fileName) 
    { 
     ...... 
    } 
} 

public class SteelPipeShape : SteelShape 
{ 
    public SteelBeamShape(string SteelBeamNominalValue) 
    { 
     this.xmlDataPath = SteelShape.commonPath; 
     XDocument doc = SteelShape.LoadData("steelbeamshape.xml"); 
     // do stuff .... 
    } 
} 
public class SteelBeamShape : SteelShape 
{ 
    public SteelPipeShape(string SteelPipeNominalValue) 
    { 
     // look up some properties base on nominal value in XML tables 
     this.xmlDataPath = SteelShape.commonPath; 
     XDocument doc = SteelShape.LoadData("steelpipeshape.xml"); 

     // do stuff .... 
    } 
} 
+1

谢谢史蒂夫,这对我有帮助。我不得不做一些关于静态变量的研究。我一直认为静态对于实例来说是静态的。我没有意识到可以像这样使用静态变量。在创建实例之前,我也采用了pln的加载表的建议。所以我在最后的解决方案中做了什么,是设置我的对象类,以便他们将继承一个共同的基类,正如你所建议的。但是我使静态成员成为一个DataSet,它被加载到我的主程序的初始化部分。那样我就不用每次都加载它了,因为我们建议使用 – 2014-10-20 17:23:30

+0

看来你已经很好地理解了静态的概念。好工作 – Steve 2014-10-20 17:25:40

0

您可以通过这样做从公共基类继承所有Shape类,并在基类中将公共变量定义为protected staticpublic static

public abstract class BaseShape 
{ 
    public static string mSomeVar = "SomeValue"; 
} 

public class SteelBeamShape : BaseShape 
{ 
    // constructor 
    public SteelBeamShape(string SteelBeamNominalValue) 
    { 
     // look up some properties base on nominal value in XML tables 
     this.xmlDataPath = this.mSomeVar; 

     // do stuff .... 
    } 
} 

public class SteelPipeShape : BaseShape 
{ 
    // constructor 
    public SteelPipeShape(string SteelPipeNominalValue) 
    { 
     // look up some properties base on nominal value in XML tables 
     this.xmlDataPath = this.mSomeVar; 

     // do stuff .... 
    }  
} 
0
public class Lib 
{ 
    private static string _commonVarialble; 

    public static string CommonVarialble 
    { 
     get { return _commonVarialble; } 
     set { _commonVarialble = value; } 
    } 

    public class SteelBeamShape 
    { 
     // constructor 
     public SteelBeamShape(string steelBeamNominalValue) 
     { 
      // look up some properties base on nominal value in XML tables 
      this.xmlDataPath = CommonVarialble; 

      // do stuff .... 
     } 
    } 
} 

现在,您可以设置CommonVarialble您的类库外

5

我会考虑先加载XML数据,然后既可以通过构造函数或类中的方法进行实际发送数据工作。

否则,你可能在你的类中做了基本相同的事情(加载数据,查找XML数据等)中的代码重复。

+0

你说得对。我看到,如果每次创建对象时不需要重新加载表,这将如何更高效。我提出了你的建议,并将其纳入我的解决方案中。 – 2014-10-20 18:20:57

相关问题