Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the type of another nested class from the same "parent" class

I'm attempting to make a namespace for the first time while learning programming. I am hoping there is a way around the problem I've run into that isn't particularly messy, but essentially I have a class object that keeps two dictionaries of two nested class objects. I need to be able to pass the Dictionary of NestedClassA to NestedClassB or to allow NestedClassB to access it in some way... Here is an example of what I'm trying to do:

namespace MyFirstNamespace 
{
    public class BossClass 
    {
        public Dictionary<int, NestedClassA> DictionaryA = new Dictionary<int, NestedClassA>();
        public Dictionary<int, NestedClassB> DictionaryB = new Dictionary<int, NestedClassB>();

        public class NestedClassA { ...arbitrary class definition... }

        public class NestedClassB 
        {
            public Dictionary<int, NestedClassA> PassedDictionary;

            public NestedClassB() { }

            public NestedClassB(Dictionary<int, NestedClassA> tempDic))
            {
                PassedDictionary = tempDic;
            }
        }


        public BossClass() { ... arbitrary constructor ... }

        ...arbitrary dictionary population methods...

        function void CreateAClassBInstance()
        {
            DictionaryB[n] = new NestedClassB(n, DictionaryA);
        }
    }
}

My problem seems to be that I can't typecast "NestedClassA" within "NestedClassB" because it doesn't recognize the type. Is it possible to access the "NestedClassA" type within B? Nothing I've tried has worked. Do I have to pass the instance of "BossClass" so I can reference type by "Dictionary<int, MyFirstNamespace.BossClassInstance.NestedClassA>"?

Any help would be appreciated. To be clear, I want a REFERENCE variable passed to NestedClassB of a Dictionary of all NestedClassA members so they can be manipulated by NestedClassB. It can't be a clone. I know this seems like ridiculous implementation, but it seems the most effective, if it's possible, for what I'm trying to do.

EDIT: maybe I shouldn't be nesting them at all, but it would make them much easier to serialize, which is why I really wanted to do it this way.

(EDIT - fixed typo where I forgot to insert "public" before constructors.)

like image 904
ThisHandleNotInUse Avatar asked Nov 02 '22 08:11

ThisHandleNotInUse


1 Answers

There doesn't seem to be anything particularly wrong with your implementation of NestedClassB other than you need to make your constructors public if you wish to instantiate an instance of NestedClassB. By default in .Net, objects are passed by reference to function parameters, so you will have the same instance of Dictionary<int, NestedClassA> in NestedClassB.

Here is the adjusted class:

    public class NestedClassB 
    {
        private readonly Dictionary<int, NestedClassA> _PassedDictionary;

        public NestedClassB() { }

        public NestedClassB(Dictionary<int, NestedClassA> tempDic) {
            _PassedDictionary = tempDic;
        }

        public Dictionary<int, NestedClassA> PassedDictionary {
            get { return _PassedDictionary; }
        }
    }

Note that I changed PassedDictionary to a property instead of a member variable. Most serializers will ignore member variables and only serialize properties. If you need to deserialize, you'll need to remove the readonly from the private member variable and add a setter.

The function at the bottom of your code snippet doesn't look right. You'll want to make it look like:

private void CreateAClassBInstance()
{
    DictionaryB[n] = new NestedClassB(DictionaryA);
}
like image 145
ohiodoug Avatar answered Nov 15 '22 04:11

ohiodoug