Shyju
Shyju

Reputation: 218722

Accessing value of a non static class in a static class

in ASP.NET C#., How can i set the variable values of a static class from the value present in a non static class .edx : I have a static class called staticA and a non static class called B which inhertits system.WEb.UI.Page. I have some values present in the class B ,which i want to set as the property value of the static class A so that i can use it throughout the project

Any thoughts ?

Upvotes: 0

Views: 1073

Answers (3)

Brij
Brij

Reputation: 6122

See following example:

 public static class staticA 
{
    /// <summary>
    /// Global variable storing important stuff.
    /// </summary>
    static string _importantData;

    /// <summary>
    /// Get or set the static important data.
    /// </summary>
    public static string ImportantData
    {
        get
        {
            return _importantData;
        }
        set
        {
            _importantData = value;
        }
    }
}

and in classB

public partial class _classB : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 1.
        // Get the current ImportantData.
        string important1 = staticA.ImportantData;

        // 2.
        // If we don't have the data yet, initialize it.
        if (important1 == null)
        {
            // Example code only.
            important1 = DateTime.Now.ToString();
            staticA.ImportantData = important1;
        }

        // 3.
        // Render the important data.
        Important1.Text = important1;
    }
}

Hope, It helps.

Upvotes: 1

Alex Martelli
Alex Martelli

Reputation: 881555

The "proper" approach would be to pass your specific instance of B (don't confuse a class and its instances!!!) to a method of A which will copy whatever properties (or other values) it needs to.

Upvotes: 2

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

staticA.AValue = b.BValue

Upvotes: 3

Related Questions