+ 2

Is a object that is a lower in hierachy allow to know about property values in a class that is higher in hierachy

I have a class Group, which contains a object of class Person. Class Person has a property that has different min and max value, based on a property in class Group. So in Class Person I need to know this value. Is there a way to do this or is this a design mistake, that should be solved different. https://code.sololearn.com/cU8767CpN9Pa I tried to make my sample as clear as possible. Since I did not want to use the real code. It is important to understand that IsTechnical is really a property of group. You want all persons to belong to that group, use the same value. They are either all technical or they are not. Therefore I do not want to move this property to the person class.

16th Dec 2019, 8:45 PM
sneeze
sneeze - avatar
9 Respostas
+ 2
To class Person add a member of type Group as parent, then instead of freely adding objects of type Person to the list, make it through a function that also sets the parent of Person to the Group to which it is added. See here a skeleton example: https://code.sololearn.com/cHiVRHl3845e
4th Jan 2020, 8:23 PM
Selin Genkur
+ 2
Thanks for answering my question. It is possible to do it this way, I altered my code and it works https://code.sololearn.com/c3KHVUu0fD8v It does have a side effect though, now I can print the list off users as a field of 1 persons.
4th Jan 2020, 8:54 PM
sneeze
sneeze - avatar
+ 2
That's funny, I didn't think of that :) But I did had in mind to suggest making List<Person> private so that you don't accidentally add a Person directly somewhere without using the designated method for it. Then printing the list as a field of one person wouldn't work anymore.
4th Jan 2020, 9:08 PM
Selin Genkur
+ 1
That would a good solution. To avoid the side-effect.
4th Jan 2020, 9:11 PM
sneeze
sneeze - avatar
+ 1
Sorry, I didn't think that through: if you do need to print the list somewhere, you'd have to also make a method to expose it, and then it would work again to print it as a field of one person. I was also thinking of a more convoluted solution: add to Person a field isTechnical, make isTechnical in Group private and only set it through a method that would also keep the isTechnical members of the Persons list in sync. See here what I mean: https://code.sololearn.com/cmP9qPIiJ5Q5/
4th Jan 2020, 9:28 PM
Selin Genkur
+ 1
But I think the first solution is best, the "side effect" that you mention is normal and not a problem, imo. More of a problem could be the necessity to expose isTechnical to everyone just so that Person can access it. I was wondering if the 'friend class' concept from C++ exists in C#, but seems it doesn't.
4th Jan 2020, 10:09 PM
Selin Genkur
+ 1
Good point. Have to think about that. Is the parent property a object or just a reference ? If it is just a reference no problem, if it is a object it could seriously increase the memory usage.
4th Jan 2020, 10:30 PM
sneeze
sneeze - avatar
+ 1
It's reference. That was my first concern too, and I remembered that classes in C# are reference types.
4th Jan 2020, 10:33 PM
Selin Genkur
0
You know, David Carroll could say if there's a better way. Sorry to bother David, idk anyone else who knows C#.
4th Jan 2020, 11:04 PM
Selin Genkur