function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Enrico devEnrico dev 

Share Map between Outer and inner class

 I want to implement the following setup:

public class Outer{
      public Map<string,string> PackageTransportUnitType { get; set; }
      public Outer(){
          PackageTransportUnitType=new Map<string,string>();
          Inner MyInner=new Inner();
          system.debug('My Map'+PackageTransportUnitType);
     }



                      public class Inner{
                                public Inner(){
                                 //NOT POSSIBLE TO ACCESS PackageTransportUnitType 
                                    PackageTransportUnitType.put('test','test')
                                }
                      }
}
I want to share the map "PackageTransportUnitType " between inner and outer class in this way:

-Outer: create and use map

-Inner: Fill values in the map

I' ve tried the above way but the map is not visible in the inner class. How can accomplish this?

Thanks in advantage for any advice.
logontokartiklogontokartik
I dont think you can basically do that, you have 2 options here as far as I know.

1. Change the Map to a Static variable so that it can be used anywhere in the context. 
2. Or change your structure of class to make the Outer class as virtual class and extend it with your innerclass, then you can use Super.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_super.htm


SarfarajSarfaraj
Hi Enrico

Use this,
public class OuterClass{
      public Map<string,string> PackageTransportUnitType { get; set; }
      public OuterClass(){
          PackageTransportUnitType=new Map<string,string>();
		  //Pass the map to the constructor of the inner class
          InnerClass MyInner=new InnerClass(PackageTransportUnitType);
          system.debug('My Map'+PackageTransportUnitType);
     }

	public class InnerClass{
		public InnerClass(Map<string,string> PackageTransportUnitType){
			//POSSIBLE TO ACCESS PackageTransportUnitType 
			PackageTransportUnitType.put('test','test');
		}
	}
}