Hi.
Let’s say that I have two classes :
ChildClass {
public int someNumber
void SomeMethod()
{
Debug.log ("this is the value of someParentNumber")
}
}
Parent Class {
public int someParentNumber
public List<ChildClass> ChildInstance = new List <ChildClass>();
}
public static List InstancesofParrentClass = new List();
How can I now access someParentNumber from Child Class ?
If only way to create reference is to pass whole ParentClass to ChildClass in constructor, please tell me is this costly?
Because class that I want to access this number from has a lot of fields, methods etc. It’s really large and I feel bad to pass it to child class, because I have more than 20 instances of Child Class per one Parent Class, which will also have around 20 instances at least.
Please note that I would access this by typing :
newInstanceOfParentClass[0].ChildInstance[0].SomeMethod() // that needs someParentNumber to calculate something
Any way to access this integer from Child class will be helpful, besides passing whole Class to Child. I don’t know how to create an reference to that field.
Really important note : I need this field in Child class to auto refresh itself, so I don’t need solution that just gets value from parent class once and stores it in variable.
I need to get it every time I call SomeMethod().
Thanks!
Your two classes aren’t related at all. Your “ParentClass” just has some fields which hold references to instances of your “ChildClass”. The ChildClass has no ides that ParentClass even exists, nor has it a reference to it. Furthermore one ChildClass reference could be in the list of multiple ParentClasses. If you want to be able to back reference the “responsible” parent from the child class, add a field to your child class and assign the parent class reference when you create the child class.
Example:
// C#
public class ParentClass
{
public int someParentNumber
public List<ChildClass> childs = new List<ChildClass>();
}
public class ChildClass
{
public ParentClass parent;
public ChildClass(ParentClass aParent)
{
parent = aParent;
}
public void SomeMethod()
{
Debug.Log("My parents number is:" + parent.someParentNumber);
}
}
ParentClass P = new ParentClass();
P.childs.Add(new ChildClass(P));
P.childs.Add(new ChildClass(P));
P.childs.Add(new ChildClass(P));
P.childs[1].SomeMethod();
In this example i used an constructor parameter to set the parent reference when the class is created. If this reference is required (must have) by the child it’s best to aquire it through the constructor. If this question was about classes derived from MonoBehaviour you should have made the question and the example more clear as it’s quite abstract pseudo code what you have there.
Not sure how much of your code is pseudo code vs actual code, but in your provided code you are missing the actual inheritance:
class Parent
{
}
class Child: Parent
{
}
Also, your parent should probably not have any knowledge of the child classes.
I’d recommend this link as well: http://www.dotnetperls.com/inheritance