Set property of derived class on base class' instance

base class and derived class in c# example
abstract class properties c#
c# extend class
c# inheritance constructor
c# abstract class
c force class to be inherited
class vs abstract class c#
c# interface

I have a base class like this:

class Document {
    int number {get; set;}
    double sum {get; set;}

... and two derived classes:

class Bill : Document {
    double advance {get; set;}

class Quote : Document {
    double discount {get; set;}

At compile time, I know that I'll be dealing with an object of type Document. However, it's only at runtime that I know whether it will be a Bill or a Quote.

I need to perform specific operations depending on the type, e.g.:

if (myDoc.GetType() == typeof(Bill)) {
   myDoc.advance = 1000; //<-- can't find "advance"

.. but I can't compile like that as the properties of the derived class is not accessible from a base instance.

What is the proper way of doing that? Here's a fiddle to play around.

myDoc.advance can only compile if the compiler can tell at compile time that myDoc is a Bill. Otherwise, myDoc is just a Document and that type does not have an advance member.

Doing a type check like that will not modify the static type of myDoc. Instead, you would have to type cast it into a Bill first:

Bill myBill = (Bill)myDoc;
myBill.advance = 1000;

Note that instead of using GetType() which checks the exact runtime type, you should be using the is operator to verify that myDoc is a Bill. You can even use the newer pattern matching syntax from C# to make this nicer:

if (myDoc is Bill myBill) {
    myBill.advance = 1000;

Finally, please note that the common convention is to write properties in C# in PascalCase (i.e. Advance, Number, Sum and Discount).

Also note that class members are private by default, so you still wouldn’t be able to access those properties. So you would have to add a public in front of the property definitions.

Inheritance, Inheritance enables you to create new classes that reuse, extend, and //​Properties. protected int ID { get; set; } protected string Title { get; set; }  RE: set value in base class, access from derived. The problem is that you are creating an object of type Family and setting its property. The object of type Father you are creating is totally separate from the Family object instance you created earlier.

Pattern matching is a good way to handle this if you are using C# 7.0+

if(myDoc is Bill bill)
    bill.Advance = 1000;
else if(myDoc is Quote quote)
    quote.Discount = 1000;

How to define abstract properties, An abstract property declaration does not provide an implementation of the how to implement the abstract properties inherited from a base class. name; public Shape(string s) { // calling the set accessor of the Id property. The property Name in the derived class hides the property Name in the base class. In such a case, the new modifier is used in the declaration of the property in the derived class: public new string Name The cast (Employee) is used to access the hidden property in the base class: ((Employee)m1).Name = "Mary";

You can simply cast it:

if (myDoc.GetType() == typeof(Bill))
    ((Bill)myDoc).advance = 1000;

Setting a Base Class property from Derived class, Setting a Base Class property from Derived class . Learn more about oops, c++, software development, matlab, object oriented, property setting  The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA,

Inheritance, Classes can also add property observers to inherited properties in order to be Swift classes do not inherit from a universal base class. You can set the hasBasket property to true for a particular Bicycle instance after that instance is created:. Ok so, I've run into an interested and probably simple problem. I have a base class that is inherited by another class (child). I have the same parameterless constructor in the base and the child. I would like to set defaults in the child that propagate into the base properties. I would like to do something like this:

Handbook - Classes, This example shows the most basic inheritance feature: classes inherit properties and methods from base classes. Here, Dog  I have made the following clone method, using generics, which can also be used for base class to derived class casting: /// <summary>Performs a shallow clone of an object by copying property values using reflection.

Base class with common properties, Yes its 'normal', whether it's correct or not is a harder question to answer. You should use inheritance where objects are conceptually a  Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Change type of property in derived class. My current setup works as

  • Thanks for the solution and for going the extra-mile with naming conventions and accessors!