Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class inheritance: recreate base class items (or instance) from a property of the inherited class

  • I have a class A that is inherited from B.
    • A as some readonly properties that I want to modify from B
    • Hiding those properties with new is not a suitable option, cause the base class has some functions that use its own properties...
    • Can't use the override keyword, cause the properties are not marked as abstract, virtual nor override

So I'd like to know whether from the inherited class (B) I can totally recreate the actual instance of my object to access those readonly properties.

For example and for a better explaination, for a class inheriting Tuple, if it was possible, I would do something like this:

    public new T3 Item3
    {
        get { return item3; }
        set 
        {
            item3 = value;
            base = new Tuple<T1, T2, T3>(Item1, Item2, Item3); // Not valid
        }
    }

I can't see how to do this?

like image 246
GianT971 Avatar asked Oct 16 '11 23:10

GianT971


People also ask

Can base classes inherit from another base class?

Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.

What all is inherited from the base class?

1) The base class's constructors and destructor. 2) The base class's friend functions. 3) Overloaded operators of the base class.

What does it mean when a class inherits another class?

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

How do you access an inherited class from base class?

To access an element of a base class from an inherited class, you need to use the base keyword. The base keyword is used in inherited classes in the following cases: when from the constructor of a derived class you need to call the constructor of the base class.


1 Answers

A tuple is immutable, so you can't change its values. When you have immutable objects, the way to change them is to return a new object with the desired properties changed. So if you want to stick with tuples, you could do something like this:

public static class TupleExtensions {
  public static Tuple<T1, T2, T3> 
    WhereItem3Is<T1, T2, T3>(this Tuple<T1, T2, T3> self, T3 newValue) {
    return Tuple.Create(self.Item1, self.Item2, newValue);
  }
  // other methods for Tuple<,,> or other Tuples...
}

And use it like this:

var t = Tuple.Create(1, 2, 3);
// ...
t = t.WhereItem3Is(4);

But it's a little bit painful to write all those methods. So if you need many of them, better just do this:

var t = Tuple.Create(1, 2, 3);
t = Tuple.Create(t1.Item1, t1.Item2, 4);

You could even have a wrapper type that you'd use to reference the tuple from different places in your code, so that any "changes" could be visible:

var t = Tuple.Create(1, 2, 3);
var r = new Ref<Tuple<int, int, int>>(t);
// share r ...
r.Value = Tuple.Create(r.Value.Item1, r.Value.Item2, 4);

...

public class Ref<T> {
  public T Value { get; set; }
  public Ref(T value) { Value = value; } 
}

All this, though, feels very awkward. Maybe you could better explain the essential problem you're having so that better answers could be provided. Maybe you don't really need a tuple after all, just something more specific to your domain.

like image 110
Jordão Avatar answered Nov 13 '22 06:11

Jordão