-1

First of all, please read through the question before concluding it is a dup of Member '<method>' cannot be accessed with an instance reference.

Now, my question is different because my Static Member is another class:

using System;

public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }
    public static void Drive() { Console.WriteLine("Driving"); }
    // public static event bool RunOutOfGas;

    // Other non-static fields and properties...
}

class SimpleClass
{
    // Static variable that must be initialized at run time. 
    static readonly long baseline;
    public static Automobile A1;

    // Static constructor is called at most one time, before any 
    // instance constructor is invoked or member is accessed. 
    static SimpleClass()
    {
        baseline = DateTime.Now.Ticks;
        A1 = new Automobile();
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Automobile");

        Automobile.Drive();
        Console.WriteLine(Automobile.NumberOfWheels);

        Console.WriteLine("SimpleClass");
        Console.WriteLine(SimpleClass.A1.NumberOfWheels);
        Console.WriteLine(SimpleClass.Automobile.NumberOfWheels);

    }
}

Either SimpleClass.A1.NumberOfWheels or SimpleClass.Automobile.NumberOfWheels (or even SimpleClass.NumberOfWheels), will give me errors.

How to access A1.NumberOfWheels under SimpleClass?

Conclusion: So if you use a static class instead of a C# simple type, there will be no way to access its static members (A1.NumberOfWheels) under the root class (SimpleClass), due to the limitation C# Static Members access rule. I ask the question because I think it is an obvious design defect, however, nobody address this specific issue and quickly swipe it under the carpet.

Probably the larger question is just that: What, exactly, are you trying to do?

@RufusL, good question. My understanding of the static variable or its purpose, is to share a single-point of truth among all its class objects. Using a very simple example to explain it, suppose I have a bunch of single-digit multiplier objects, which are in charge of calculating multiple of two single-digit numbers. The fastest way is not to calculate it each time but to store a two-dimension single-digit multiple table in their static variable, so no real multiple is needed, only lookup. This will dramatically increase the performance.
Now,

  • it doesn't make sense that such static variable can only be C# predefined simple types, and can't be my own defined types.
  • Neither it make much sense that such static variable within a class can only be one level deep, and can't get any deeper.
xpt
  • 20,363
  • 37
  • 127
  • 216
  • 1
    "or even `SimpleClass.NumberOfWheels`" Why do you think this is supposed to work ? `NumberOfWheels` is declared in another class... – Selman Genç Sep 21 '18 at 23:51
  • 1
    Unless you are doing something specific, `static` methods and properties should pretty much be avoided entirely. You seem to be in the habit of using them all over the place and this probably means you are "doing it wrong." In your case you probably want a normal, instanced, non-static `Automobile` class, and possibly a reference to it held in a static variable or as a local variable in `Main()`. Making everything static is going to be a huge problem if you ever need to be able to handle more than one automobile at a time. – John Wu Sep 21 '18 at 23:59
  • 1
    Yes it is a duplicate. No, just you using your own, different class and using a slightly different "path" to access the problematic static member does not make it not a duplicate. (An simplified equivalent - and quite silly, i have to admit - example would be: "_Why is this not working: 5*3/0? It is not a duplicate of the other question talking about division by zero because i do a multiplication first. See..._") –  Sep 22 '18 at 00:01
  • You have a static member of an *instance* type. You even new up the instance in your constructor. Try doing `int x = A1.NumberOfWheels;` inside the `SimpleClass` constructor - it doesn't work there either. For more info, see the duplicate question... :) – Rufus L Sep 22 '18 at 00:07
  • @RufusL, please show me how to use a class variable without new up an instance first. – xpt Sep 22 '18 at 12:34
  • If you want to provide access to a static member of another class (`Automobile`) through your class (`SimpleClass`), then you could provide a property wrapper around it: `public int NumberOfWheels { get { return Automobile.NumberOfWheels; } set { Automobile.NumberOfWheels = value; } }`. But it makes little sense to have an instance class like `SimpleClass` expose a property that is a static field of another class. In fact, it doesn't make much sense to have `NumberOfWheels` as a `static` member in the first place. This whole design is very strange. – Rufus L Sep 22 '18 at 18:09
  • I think you may be misunderstanding the meaning and purpose of `static` vs `instance` properties, and you also may want to research `inheritance` and `interface` depending on what you're trying to do. Probably the larger question is just that: *What, exactly, are you trying to do?* – Rufus L Sep 22 '18 at 18:13
  • @RufusL, good question. Explained in OP. – xpt Sep 23 '18 at 03:25
  • static means that it applies to the `type` itself, not an `instance` of the type. When you do `public static Automobile A1;` you now have a static member of your class which is an instance of `Automobile`, allowing other classes to interact with this instance. But you cannot access it's `static` members, because static members do not belong to instances. – Rufus L Sep 23 '18 at 19:01

2 Answers2

2

SimpleClass.A1.NumberOfWheels gives you an error because A1 is an instance and you are trying to access a static field through an instance.

SimpleClass.Automobile.NumberOfWheels gives you an error because there is no public static member declared as Automobil in the SimpleClass.

Only way to access NumberOfWheels is Automobile.NumberOfWheels.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

A1 maybe a static member of SimpleClass, but that static member is still a reference to a particular instance of the Automobile type. If you need access to A1.NumberOfWheels, maybe the NumberOfWheels property should not be static.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794