- Home /
Access Derived variable passing base as ref
So I think this will work but not really sure if it's the best way or if it's bad design or whatever have you. Just looking for some input if this is outright bad or if it's going to cause problems down the road.
base class:
public class Employee
{
public string name;
public float salary;
//etc...
}
Derived class:
public class Manager : Employee
{
public List<Employee> employees;
//etc...
}
Somewhere else in the nether:
public List<Employee> allEmployees //including managers
public void StuffToDo()
{
foreach (Employee emp in allEmployees)
{
DoUnspeakableThings(emp)
}
}
public void DoUnspeakableThings(Employee emp)
{
someText.text = emp.name;
if (emp is Manager)
{
Manager mgr = emp as Manager;
someOtherScript.DoMoreUnspeakableThingsToManagerEmployees(mgr.employees);
}
}
So, is this acceptable? Will it work? Am I just asking for trouble?
You mention passing something as a reference; I don't see that here. Question is confusing or something was lost in translation.
Anyway, once you deter$$anonymous$$e something's type or you successfully cast it to a type, you're operating on it as that type and thus should have no issue accessing the derivative's unique properties, methods, etc. Is this your question?
I don't see anything obviously wrong with the code bar a missing semicolon at the end of the first line of the final snippet.
Have you tried it? What happened? What makes you think you might be "asking for trouble"?
Well I suppose part of the concern was when looking into it, I found a couple random posts on stack exchange clai$$anonymous$$g that accessing derived properties by passing the base class could be poor design with no real explanations or solution.
The concern is more around the idea of storing the derived class in a list of the base class and throwing them around willy nilly as a base class. What I consider might be asking for trouble is when it comes to manipulating those members of the derived class.
Apologies for any lack of clarity.
As far as reference, I thought classes were passed by ref by default?
On the ref thing; ref is a keyword, whereas I'd expect someone to use the whole word unless they were talking about that keyword. Little misunderstanding,
If it's convenient, I like to have a manager for collections of like objects. Or any time several lists will cross-reference and overlap, whatever, so I can query them as needed with ease. These managers always wind up involving a list of all derrivative instances just like you're doing, but I like collections that exist in the forms in which I want to frequently access them, too. Costs memory, may or may not improve performance (depends on access needs and frequency of loop searches, etc), but it gives you a stronger command of your data.
Having all your derivative instances in the same collection as base-class instances isn't inherently bad, but it's worth considering alternatives simply for style and command. If you build a helper class to hold and manage your set o' collections, you can make them do whatever you want - with syntactic elegance.
Your answer
Follow this Question
Related Questions
"Unity can't serialize derived classes that are stored in a base-class pointer", why is this so? 0 Answers
Can I access a deriving class through a base/parent class? 2 Answers
How to get "transform.position" from Base Class in realtime 1 Answer
Is the class derived/inherited from generic list serializable? 1 Answer