locked
can i make object of a private class as public RRS feed

  • Question

  • suppose i have two class in first class i want to use the object of second class(private class) as public in first class(Constructor in second class is internal).another thing which i have is can i access the internal variables of second class through the public properties created in first class.
    Thursday, March 1, 2012 1:08 PM

Answers

  • If you want to use a class in any public method, then the class itself must be public.

    > can i access the internal variables of second class through the public properties created in first class

    No. 

    The first class could create an instance of the second class and access public (and internal) properties on that instance.  You could also expose those properties through additional properties on the first class.

    class class1
    { 
       public class1() 
       { 
          _instance = new class2(); 
       }
       
       public string Title 
       { 
          get
          {
             return _instance.Title ; 
          } 
       } 
    
       private class2 _instance = null ; 
    
    } 
    
    class class2 
    {
       public string Title { get { ... } } 
    }


    Regards, Phill W.

    • Proposed as answer by Bob Wu-MT Tuesday, March 6, 2012 6:04 AM
    • Marked as answer by Bob Wu-MT Tuesday, April 3, 2012 3:34 AM
    Thursday, March 1, 2012 1:26 PM
  • Hi Syed,
    I agree with Phill.
    I think you should read the Access Modifiers.
    public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
    private: The type or member can be accessed only by code in the same class or struct.
    protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
    internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
    protected internal: The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
    However, you can access private constructors, methods, fields, and properties with Reflection. For more details, see http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx.
    If there is anything unclear, please let us know.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Bob Wu-MT Tuesday, April 3, 2012 3:34 AM
    Tuesday, March 6, 2012 6:05 AM

All replies

  • If you want to use a class in any public method, then the class itself must be public.

    > can i access the internal variables of second class through the public properties created in first class

    No. 

    The first class could create an instance of the second class and access public (and internal) properties on that instance.  You could also expose those properties through additional properties on the first class.

    class class1
    { 
       public class1() 
       { 
          _instance = new class2(); 
       }
       
       public string Title 
       { 
          get
          {
             return _instance.Title ; 
          } 
       } 
    
       private class2 _instance = null ; 
    
    } 
    
    class class2 
    {
       public string Title { get { ... } } 
    }


    Regards, Phill W.

    • Proposed as answer by Bob Wu-MT Tuesday, March 6, 2012 6:04 AM
    • Marked as answer by Bob Wu-MT Tuesday, April 3, 2012 3:34 AM
    Thursday, March 1, 2012 1:26 PM
  • Hi Syed,
    I agree with Phill.
    I think you should read the Access Modifiers.
    public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
    private: The type or member can be accessed only by code in the same class or struct.
    protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
    internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
    protected internal: The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
    However, you can access private constructors, methods, fields, and properties with Reflection. For more details, see http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx.
    If there is anything unclear, please let us know.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Bob Wu-MT Tuesday, April 3, 2012 3:34 AM
    Tuesday, March 6, 2012 6:05 AM