brainfree
brainfree

Reputation: 827

in C#, can a derived value be referred to by both static and instance methods/properties?

I'm quite new to C#, and I've been banging my head against a wall trying to figure out how to implement a certain design pattern related to instance and static properties returning the same value out of a derived class. It seems like there might be restrictions in place keeping this from happening, so I wanted to explain my issue to see if anyone has thoughts on how to solve this problem.

public class Resource {
    protected static float s_unitMass = 1.0f;
    protected static string s_name = "<Resource>";
    public int quantity;

    // Class Convenience
    static public float GetUnitMass() {
        return s_unitMass;
    }
    static public string GetName() {
        return s_name;
    }

    // Instance Convenience
    virtual public float totalMass {
        get { return s_unitMass * quantity; }
    }
    virtual public float unitMass {
        get { return s_unitMass; }
    }
    virtual public string name {
        get { return s_name; }
    }
}

public class Iron : Resource {
    new protected static s_unitMass = 2.0f;
    new protected static s_name = "Iron";
}

This code very much does not work (the values for the base class Resource are always returned), but I'm writing it out this way to indicate what I would like to do... Have a value that can be referred to by both:

string name = Iron.GetName();
float unitMass = Iron.GetUnitMass();

and

Iron iron = new Iron();
string name = iron.name;
float unitMass = iron.unitMass;
float totalMass = iron.totalMass;

Upvotes: 0

Views: 75

Answers (1)

user2864740
user2864740

Reputation: 61885

If this is really desired, then

// Have a [static] singleton Iron instance, but doesn't prevent
// creation of new Iron instances..
static class ResourceTable {
    public static Iron Iron = new Iron();
};

// Just an Iron instance; it doesn't matter where it comes from
// (Because it is a 'singleton' the SAME instance is returned each time.)
Iron iron = ResourceTable.Iron;    // or new Iron();
                                   //    [^- object ]

// .. and it can be used the same
string name = iron.name;           // or ResourceTable.Iron.name, eg.
float unitMass = iron.unitMass;    //    [^- object too!  ]
float totalMass = iron.totalMass;

Now, for some notes.

  1. Generally a singleton doesn't allow "alternative methods of creation"; and mutable singletons are .. icky; and,

  2. This is over-specialization of types (e.g Iron, Feather, ..); and,

  3. The element type (which is relates to a particular mass eg.) should probably be separate from the quantity, as there may be multiple quantities associated with a resource throughout a problem.

Consider:

static Resource {
    public string Name { get; set; }
    public float UnitMass { get; set; }
}

static Bucket {
    public Resource FilledWith { get; set; }
    public int Quantity { get; set; }
    public float TotalMass {
      get { return FilledWith.UnitMass * Quantity; }
    }
}

static class ResourceTable {
    public Resource Iron =
      new Resource { Name = "Iron", UnitMass = 1 };
    public Resource Feather =
      new Resource { Name = "Feather", UnitMass = 0.1 };
}

var aBucket = new Bucket {
    FilledWith = ResourceTable.Iron,
    Quantity = 100
};

Upvotes: 3

Related Questions