Results 1 to 5 of 5

Thread: [RESOLVED] How to Restrict a class from being instantiated from particular class only

  1. #1

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Resolved [RESOLVED] How to Restrict a class from being instantiated from particular class only

    For illustration purpose

    I have a class called ChildFactory & I need to restrict that, this class Can be called only by the Parents Class, but not by other classes
    how to restrict the implementation in this way.
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Restrict a class from being instantiated from particular class only

    Off the top of my head, there's no way to restrict the instantiation of one class to another specific but what you could do is put those two classes in their own library and then declare the constructor(s) of the one class internal, which means that that class can only be instantiated by classes declared in the same assembly. That's how it is that you cannot create a DataRow object directly in your own code but the DataTable class can.

  3. #3

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: How to Restrict a class from being instantiated from particular class only

    Thank you
    vb.net Code:
    1. namespace ProductionOperations{
    2.     public class Parents
    3.     {
    4.  
    5.  
    6.         public void Do_ChildDesign()
    7.         {
    8.             ChildFactory Cf = new ChildFactory();
    9.             Cf.DesignChild();
    10.         }
    11.     }
    12.  
    13.  
    14.     internal class ChildFactory
    15.     {
    16.         internal ChildFactory()
    17.         {
    18.  
    19.  
    20.         }
    21.  
    22.  
    23.         private void DesignChild()
    24.         {
    25.             return;
    26.         }
    27.     }
    28.  
    29.  
    30. }
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Restrict a class from being instantiated from particular class only

    For the record, I wasn't suggesting that you make the class internal; just the constructor. By making the class internal to, you restrict it to be used at all only within that assembly. If that works for you then you probably didn't need another assembly in the first place and you could have just declared the one class private inside the other. In both cases, you end up with ChildFactory being known and able to be used only by Parents.

  5. #5

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: [RESOLVED] How to Restrict a class from being instantiated from particular class

    you could have just declared the one class private inside the other. In both cases, you end up with ChildFactory being known and able to be used only by Parents.
    Yes sir it worked for me
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width