Mike Sportsman
Mike Sportsman

Reputation: 118

How to share a value between multiple instances of a class in C#?

I am writing a class library to be used in some engineering software at our company. The library is used to define the properties of structural steel shapes. In each of my class objects I have the need to go out to a specified file folder and look up some xml data.

How could I create a common variable I can set outside the library class and share between instances Like this: ( if the following code were possible )

    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 .... 
    }
}

}

Upvotes: 0

Views: 3146

Answers (4)

Steve
Steve

Reputation: 216263

To share a common value between different classes you need the classes derive from a common ancestor. In your case you could define

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)
     {
       ......
     }
}

then your classes derive from this base class

public class SteelPipeShape : SteelShape
{
   ....
}

whenever you need to refer to the common variable shared by your classes instances you refer to it using the ancestor class name

 SteelShape.commonPath;

So putting all together you have

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 .... 
    }
}

Upvotes: 0

pln
pln

Reputation: 1198

I would consider loading the xml data first and then send the data either through the constructors or to the methods in your classes doing the actual work.

Otherwise you probably have duplicated code in you classes doing basically the same thing (loading data, looking up in the xml data, etc).

Upvotes: 5

AskT
AskT

Reputation: 66

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 .... 
        }
    }
}

Now you can set CommonVarialble outside your library classes

Upvotes: 0

dotNET
dotNET

Reputation: 35380

You could do this by inheriting all your Shape class from a common base class and defining the common variable as protected static or public static in the base class.

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 .... 
    }    
}

Upvotes: 0

Related Questions