Basically there're 2 classes.
class Motor {
int a;
int b;
int c;
}
class Title : Motor {
int d;
int e;
}
Now a function is passed with an instance of Motor class.
bool AssignNums(Motor m)
{
Title t = (Title)m; //throws exception
//Do other things with "t"
}
And it's called from,
void AddNums()
{
Motor m = new Motor();
m.a = 23;
m.b = 78;
m.c = 109;
AssignNums(m);
}
The above line where the casting is done, doesn't work. It throws a null pointer exception.
I tried:
bool AssignNums(Motor m)
{
Title t = new Title();
t = (Title)m; // Doesn't work.
//Do other things with "t"
}
The above approach also doesn't work.
Coming from a C++ background, it's kinda difficult to understand how casting works in C#.
In C++ below code will work.
bool AssignNums(Motor* m)
{
Title* t = (Title*)m; //Will work
//Do other things with "t"
}
How can this be done in C#? Without tons of code to implement "reflection" or something like that...