locked
Access variables of a class from another class RRS feed

  • Question

  • Hi everyone,

    I've set up a class Report and within this class I define two Subclasses Read and Write and a few more variables:

    public class XlsXReport
        {
            public string FileName;
            
            int[] WorkSheetInd = new int[7] {-1, -1, -1, -1, -1, -1, -1};
            public XlsXReport()
            {   // Constructor of XlsXReport Class without Arguments
            }
            public XlsXReport(string FileName)
            {   // Constructor of XlsXReport Class with FileName as Argument
            }
            public ClRead Read = new ClRead();
            public ClWrite Write = new ClWrite();
        }
    public class ClRead
        {
            
            public ClParameters Parameters = new ClParameters();
            public ClResults Results = new ClResults();
            public class ClParameters
            {
                public object[] Test1()
                {
                    return null;
                }

    What I would like to do is accessing the accessing the variables FileName and WorkSheetInd (defined in class XlsXReport) in the method Test1 of class C1Parameters. How do I do that?

    Thanks for you assistance!


    Info im http://social.msdn.microsoft.com/Forums/en-US/user

    Friday, April 13, 2012 12:28 AM

Answers

  • Let me help you with some facts about accessing member of outer class from inner class:

    -Any inner (nested) class can be accesed only by the outer class.

    -Any inner (nested) class can be instantiated only by the outer (home).

    -Any instance of inner class has a life cycle which is shorter than or equal to the outer (home) instance.

    -Life cycle of outer instance exactly covers the life of inner instance.

    --

    Try to figure out now how this will go.


    Mitja

    • Marked as answer by schwabe111 Friday, April 13, 2012 1:31 AM
    Friday, April 13, 2012 12:53 AM

All replies

  • Let me help you with some facts about accessing member of outer class from inner class:

    -Any inner (nested) class can be accesed only by the outer class.

    -Any inner (nested) class can be instantiated only by the outer (home).

    -Any instance of inner class has a life cycle which is shorter than or equal to the outer (home) instance.

    -Life cycle of outer instance exactly covers the life of inner instance.

    --

    Try to figure out now how this will go.


    Mitja

    • Marked as answer by schwabe111 Friday, April 13, 2012 1:31 AM
    Friday, April 13, 2012 12:53 AM
  • Let me show you how this can be done in a simple example:

    public Outer
    {
        protected string MyField;
        Inner i;
        public Outer()
        {
            i = new Inner(this);
        }
    
        public Inner
        {
            public Inner(Outer o)
            {
                o.MyField = "some value";
            }
        }
    }

    -----

    Regarding your example, you should create a constructor in CIRead class that will accept a parameter of XlsXReport class. And by that reference you will be able to access members from XlsXReport class.

    Take a look into my example, and you will be good.

    Hope it helps,

    bye


    Mitja


    Friday, April 13, 2012 1:00 AM
  • Let me show you how this can be done in a simple example:

    public Outer
    {
        protected string MyField;
        Inner i;
        public Outer()
        {
            i = new Inner(this);
        }
    
        public Inner
        {
            public Inner(Outer o)
            {
                o.MyField = "some value";
            }
        }
    }

    -----

    Regarding your example, you should create a constructor in CIRead class that will accept a parameter of XlsXReport class. And by that reference you will be able to access members from XlsXReport class.

    Take a look into my example, and you will be good.

    Hope it helps,

    bye


    Mitja


    if coding like this . that will make a circel reference. the GC will be Wondering!

    there is two way for you.

    1.

       pass the Outer Class XlxsReport Instance to Test Method by parameter like below signatrue

    public object[] Test(XlxsReport pReport)

    {

    // here you can use pReport to do samethting you want.

    }

    2.

    another way is warper the variables to class

    for example

    public class VarWarper

    {

            public string FileName;
           
            public
    int[] WorkSheetInd = new int[7] {-1, -1, -1, -1, -1, -1, -1};

    }

    then

    public class XlsXReport
       
    {
            private VarWarper mVars = null;

            public XlsXReport()
           
    {   // Constructor of XlsXReport Class without Arguments

                 this.mVars = new VarWarper();

                  this.Read = = newClRead(this.mVars);

           
    }
           
    public XlsXReport(string FileName)
           
    {   // Constructor of XlsXReport Class with FileName as Argument
           
    }
           
    public ClRead Read = null;

            public ClWrite Write = new ClWrite();
       
    }
    public class ClRead
       
    {

            ClRead(VarWarper pVars)

    {

    this.Paramerters = newClParameters(pVars);

    }


           
    public ClParameters Parameters = null;

            public ClResults Results = new ClResults();

           
    public class ClParameters
           
    {

            private VarWarper mVars = null;

    ClParamters(VarWarper pVars)

    {

     

    this.mVars = pVars;

    }
               
    public object[] Test1()
               
    {

    // you can use this.mVars ...... here
                   
    return null;
               
    }


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.


    Friday, April 13, 2012 1:47 AM