Error Assigning a Derived Class to the output of a function returning a base class

Purusartha

I have read somewhere that,

A Derived class (or subclass) is an instance of its base class.

Given that, I am having problems with the following where I am inheriting a base class.

public class TransactionCategorisationRuleViewModel : CategorisationRuleViewModel
{
    public long TransactionId { get; set; }

    [Display(Name="Apply to All Transactions")]
    public bool IsApplyAll { get; set; }

}

I also have a function with the following definition:

public CategorisationRuleViewModel GetNewRule()
{
     // content
}

However, when I do this, I am getting an Error (Can not implicitly convert type CategorisationRuleViewModel to TransactionCategorisationRuleViewModel. )

TransactionCategorisationRuleViewModel vmRule = GetNewRule();
gunr2171

Let's take an easier example. We will have two classes:

class Animal { }

class Horse : Animal { }

And we are trying to call this method:

public Animal GetNewAnimal()
{
    // content
}

So if we write:

Horse newHorse = GetNewAnimal();

It gives us a casting exception. This is because GetNewAnimal returns an Animal. It might be a Horse, it might be a Pig. Since the compiler can't be sure, and you are doing implicit casting, it says "I know you want a Horse, but I can't be guaranteed that it will be, so I'm giving up."

The way you fix this is by doing explicit casting.

Horse newHorse = (Horse)GetNewAnimal();

This tells the compiler that the return type will be a Horse. However, if at runtime the return value cannot be casted as a Horse, the system will throw an InvalidCastException.


So, to map to your example, take all the Horses and Animals in my post and replace with TransactionCategorisationRuleViewModel, and CategorisationRuleViewModel, respectively.

TransactionCategorisationRuleViewModel vmRule = 
    (TransactionCategorisationRuleViewModel)GetNewRule();

This will fix your exception, but you are taking the risk that GetNewRule will actually return a TransactionCategorisationRuleViewModel. It might be easier to return a TransactionCategorisationRuleViewModel instead of the base class type.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assigning address of derived class to base class pointer

Assigning instance of a derived class to a variable of base class

Assigning base data from derived class with move produces an error

Returning derived class as base generic class

Returning Derived templated class as Base class pointer

Base Class Function Not Calling Derived Class Function

Invoke base class function as derived

Passing derived class to base function

C++: Assigning derived class pointer to base class pointer

compiler error on calling derived class function using base class pointer which is not in base class

Moq a base class function from a derived class

Passing derived class to a function of base class argument

Store Pointer to Derived Class Function in Base Class

Size of derived class in virtual base class function

Virtual function of derived class calls that of base class

Printing from a derived class with a base class function

Returning a reference to the derived class from a base class method

returning derived class from overrided method, declared as returning base copy

Call base() from derived class to execute base class function?

Calling a derived function from an array of the base class

How to access derived class member function using Base class function?

Returning any derived class type from a method outside the base and derived class

Call a function from base class that uses the member functions of derived class

Javascript: How to call base class function before overriding in derived class?

How can I friend a derived class function in the base class?

Calling derived class through base class function pointer

Can I use a derived class function if base class is called as parameter?

Call a derived class' (non-virtual) function in the base class' Destructor

How to call derived class function from base class?