Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Private inside private

Discussion in 'Scripting' started by phoda, Dec 7, 2019.

  1. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    So i have class
    Code (CSharp):
    1. class CPU
    2. {
    3.    string itemName;
    4.  
    5.    public CPU(string itemName)
    6.    {
    7.       this.itemName = itemName;
    8.    }
    9. }
    10.  
    then have dictionary
    Code (CSharp):
    1. Dictionary<string, CPU> cpu = new Dictionary<string, CPU>();
    how to create get method for dictionary? i mean easy normal way since ive been thinking you can return keys, select keys call another function that accepts keys and then returns some data but its complicated.

    TLDR how to return dictionary without making CPU class public?
    something like this
    Code (CSharp):
    1. public Dictionary<string, CPU> GetCPU()
    2. {
    3.    return ...;
    4. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Return dictionary to who? My understanding is when you declare a class as private you do so within a parent class. Then you'd declare the Dictionary as public in the private class, and it should be accessible as normal from the parent class. But it won't be accessible outside of the parent class. If you want to access the Dictionary from anywhere, well why not declare the class as public?
     
  3. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    yeah it is from another script so outside class "database" (where dictionary is stored), as to reason WHY well I wanted to use get set or custom get set as to keep scope but yeah seems ive overdone it since this is database class that hold lots of data. well guess ill use public class and dictionary and use it as such
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    In general, if you have an object of a private type, but you want to return it in a public function, you have to return it as a more generic type. For example:

    Code (CSharp):
    1. public class A
    2. {
    3.     // stuff
    4. }
    5.  
    6. private class B : A
    7. {
    8.     // stuff
    9. }
    10.  
    11. private B instanceOfB;
    12.  
    13. public A GetMyB()
    14. {
    15.     return instanceOfB;
    16. }
    The caller is actually getting an instance of the subtype, but you don't promise that it's an instance of the subtype, you just promise it's an instance of the supertype. Presumably you've set up your classes such that everything the caller needs access to is inside the definition of A, and they'll just ignore the additional stuff inside of B.

    Even if you didn't explicitly create a public base class, it's usually possible to go more generic. In Unity, a lot of your classes probably inherit from MonoBehaviour, which could also be returned as something like Component or UnityObject. In an extreme case, you can usually return something as System.Object, which all classes ultimately inherit from.

    Of course, there's not very much that the caller can do with a System.Object.