Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rasputin · Jun 02, 2015 at 08:36 AM · derived-class

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?

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Jun 02, 2015 at 06:08 AM 0
Share

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?

avatar image Bonfire-Boy · Jun 02, 2015 at 10:25 AM 0
Share

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"?

avatar image Rasputin · Jun 02, 2015 at 03:18 PM 0
Share

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.

avatar image Rasputin · Jun 02, 2015 at 03:25 PM 0
Share

As far as reference, I thought classes were passed by ref by default?

avatar image AlwaysSunny · Jun 02, 2015 at 04:27 PM 0
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Derives from MonoBehaviour or Component or is an interface. Error 2 Answers

Have array of Unit; Unit is base Class; How to access derieved class functions??? 1 Answer

Is it possible to change value on base class when the same field gets changed on it's derived class 0 Answers

CustomPropertyDrawer for derived class? 1 Answer

How to access derived class members from base class type 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges