After struggling with downcasting (see [my original post]) and with making [deep copies], I found [this eponymous article] where a suggestion is made in C++ as to how to deal with the issue. With much excitement I implemented it in C# as follows:
public partial class User
{
virtual public Employer GetEmployer() { return null; }
...
}
public partial class Employer
{
public override Employer GetEmployer() { return this; }
...
}
which I then used like this:
User u = GetUser();
Employer e = u.GetEmployer();
However (I suppose, not surprisingly), the override is never called and a null is returned.
The problem I'm trying to solve is what I gather would be a very common use-case: I get a bit of data I need to store but it's incomplete. Later I get more data and I use it to refine (downcast) my understanding of the world.
In this particular case, I get an e-mail address from someone using my website so I know they are a User
, but in general I don't know anything else about them. Later (when they fill out a form), I know that they are actually an Employer
, so I need to downcast my User
.
What is the right approach here?