Perry Battles
Perry Battles

Reputation: 31

I can't reference members of a static class from multiple dynamic classes

I'm defining a static class, and then attempting to reference its members from multiple dynamic classes in my code (all of these dynamic classes are derived from the same base class, if that helps at all). My code looks like this:

public abstract class A
{
    public A ()

    public void performSomeOperation()
}

public class B : A
{
    public override void performSomeOperation()
    {
        Console.WriteLine();
    }
}

public class C : A
{
    public override void performSomeOperation()
    {
        Console.WriteLine("This is " + MyStaticClass.stringValue);
    }
}

public static class MyStaticClass
{
    public string stringValue = "A string value";
}

In the first derived class B I cannot reference a member of MyStaticClass however, in the second derived class C I can. I can't figure out why that should make a difference. Is it the order in which the classes are defined? Do I need to place different modifiers before my class names?

I've previously worked in Python and the order in which classes were defined rarely mattered, so I'm slightly confused by this.

Upvotes: 0

Views: 93

Answers (5)

MrDustpan
MrDustpan

Reputation: 5528

You'll need to make the field static as well - or in this case a constant would probably be better:

public static class MyStaticClass
{
    public const string stringValue = "A string value"; 
}

Upvotes: 1

If you fix up all the errors you'll find that both B and C can reference MyStaticClass just fine:

using System;

public abstract class A
{
    public A () {}

    public virtual void performSomeOperation() {}
}

public class B : A
{
    public override void performSomeOperation()
    {
        Console.WriteLine("B : This is " + MyStaticClass.stringValue);
    }
}

public class C : A
{
    public override void performSomeOperation()
    {
        Console.WriteLine("C : This is " + MyStaticClass.stringValue);
    }
}

public static class MyStaticClass
{
    public static string stringValue = "A string value";
}

public static class MyApp
  {
  public static int Main()
    {
    A anA = new B();

    anA.performSomeOperation();

    anA = new C();

    anA.performSomeOperation();

    return 0;
    }
  }

ideone example here

Share and enjoy.

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Your static class won't be compiled because it has non-static member.

public static class MyStaticClass
{
    public static string stringValue = "A string value";
}

Upvotes: 0

Crono
Crono

Reputation: 10478

Your stringValue property must be static as well, and all classes must reside in the same assembly or in an assembly that references the assembly in which MyStaticClass resides.

public static class MyStaticClass
{
    public static string stringValue = "A string value";
}

Or, if the property is meant to be immutable:

public static class MyStaticClass
{
    private static string _stringValue = "A string value";

    public static string stringValue { get { return _stringValue; }}
}

Upvotes: 0

Selman Genç
Selman Genç

Reputation: 101681

You can't access it from C either. You need to define your field as static in order to access it with class name.It's not enough to make your class static only.

public static string stringValue = "A string value";

Upvotes: 0

Related Questions