Results 1 to 4 of 4

Thread: Nested Class - Prevent Instantiation of Nested Class

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    21

    Nested Class - Prevent Instantiation of Nested Class

    Hi All,

    Just a query really as i havent really written a class from scratch before so i am struggling getting my head round the log of it to be honest.

    What: I am trying to extend a class with another class, so once the main class has been instantiated then they can then choose from a series of subclasses that they can they populate properties - the final peice will have approx 150 properties so i need to break up and catagorise some properties.

    My issue here is that when i created a nested class, the user can directly instantiate the subclass which i do not want them to do, i want all the subclasses to be part of the main class and prevent direct instantiation of them.

    This is what i want them to do:
    Code:
            Dim IE1 As New IncomeExpenditure
            IE1.CustomerData.FullName = "HELLO"

    I do not want them to be able to do this.
    Code:
    Dim IE2 As New IncomeExpenditure.CustomerInfo
            IE2.FullName = "Test"
    Here is my class code so far, can someone please help me code out the ability to directly instantiate the sub class becuase there will be subs/functions in the main class that accesses properties through the sub class so i do not want them accessing it as this class will be a widely used class in several applications (once built). I have read items on Inherit, Private Default Constructors etc but when i make the change it gives me errors about accessing private members on the classes etc or the 'CustomerData' becomes unavailable.

    Appreciate any help you guys can give.
    Code:
    Public Class IncomeExpenditure
        Public CustomerData As CustomerInfo
    
    
    
        'Private
        Private m_TimeStampID As String
        Private m_ActiveStatus As String
        Private m_SourceId As String
        Private EmployeeId As Integer
    
        Public Sub New()
            'Private Constructor
            Debug.Print("Constructor")
    
        End Sub
    
    #Region "Customer Info Class"
        '##############################################################
        'Class: clsHouseHoldInfo
        'Values:    Under 14 Dependent Children
        '           14 - 16 Dependant Children (19 if in further ed)
        '           Non Dependent Household Members (Bill Contributers)
        '##############################################################
        Public Class CustomerInfo
            Private m_AC_ACCOUNT_NUMBER As Long
            Private m_AC_ACCOUNT_NAME As String
            Private m_AC_ACCOUNT_DOB As Date
            Private m_AC_ACCOUNT_EMAIL As Date
    
            Public Property DebtId() As Long
                Get
                    Return m_AC_ACCOUNT_NUMBER
                End Get
                Set(value As Long)
                    m_AC_ACCOUNT_NUMBER = value
                End Set
            End Property
            Public Property FullName() As String
                Get
                    Return m_AC_ACCOUNT_NAME
                End Get
                Set(value As String)
                    m_AC_ACCOUNT_NAME = value
                End Set
            End Property
            Public Property DateOfBirth() As Date
                Get
                    Return m_AC_ACCOUNT_DOB
                End Get
                Set(value As Date)
                    m_AC_ACCOUNT_DOB = value
                End Set
            End Property
            Public Property EmailAddress() As Date
                Get
                    Return m_AC_ACCOUNT_DOB
                End Get
                Set(value As Date)
                    m_AC_ACCOUNT_DOB = value
                End Set
            End Property
        End Class
    #End Region
    
    #Region "HouseHold Info Class"
        '##############################################################
        'Class: clsHouseHoldInfo
        'Values:    Under 14 Dependent Children
        '           14 - 16 Dependant Children (19 if in further ed)
        '           Non Dependent Household Members (Bill Contributers)
        '##############################################################
        Private Class clsHouseHoldInfo
            Private m_AC_DEPENDENT_CHILDREN_LT14 As Long
            Private m_AC_DEPENDENT_CHILDREN_14PL As Long
            Private m_AC_NO_IN_HOUSEHOLD As Long
            Private m_AC_VEHICLES_IN_HOUSEHOLD As Long
    
            Public Property Under14_Dependants() As Long
                Get
                    Return m_AC_DEPENDENT_CHILDREN_LT14
                End Get
                Set(value As Long)
                    m_AC_DEPENDENT_CHILDREN_LT14 = value
                End Set
            End Property
            Public Property Over14_Dependants() As Long
                Get
                    Return m_AC_DEPENDENT_CHILDREN_14PL
                End Get
                Set(value As Long)
                    m_AC_DEPENDENT_CHILDREN_14PL = value
                End Set
            End Property
            Public Property NonDependants() As Long
                Get
                    Return m_AC_NO_IN_HOUSEHOLD
                End Get
                Set(value As Long)
                    m_AC_NO_IN_HOUSEHOLD = value
                End Set
            End Property
            Public Property NumberOfVehicles() As Long
                Get
                    Return m_AC_VEHICLES_IN_HOUSEHOLD
                End Get
                Set(value As Long)
                    m_AC_VEHICLES_IN_HOUSEHOLD = value
                End Set
            End Property
        End Class
    #End Region
    
    End Class

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Nested Class - Prevent Instantiation of Nested Class

    You could make the constructor of the nested class Protected Friend. Your IncomeExpenditure can create an instance of it internally because it is "friend", but code that has a reference to your DLL won't be able to create a direct instance of it.

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Nested Class - Prevent Instantiation of Nested Class

    Enforcing this will be complicated, are you sure it's worth it? What is the issue if an application instantiates an instance of the class?

    You'd also want the constructor to be just Friend if you went that route, otherwise another assembly could create a class derived from your nested class. Not only would the application then be able to create instances of a type derived from your nested class (which I'm assuming is just as "bad"), but it would also be able to ask that derived class to create instances of the nested class directly (non-derived) as it has full access to the constructor through the Protected modifier.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Nested Class - Prevent Instantiation of Nested Class

    You could use interfaces:-
    vbnet Code:
    1. Public Interface IEmploymentInfo
    2.     Property Address As String
    3.     Property EmployerName As String
    4.     Property CompanyName As String
    5. End Interface
    6.  
    7. Public Class Person
    8.  
    9.     Private _employment As EmploymentInfo
    10.  
    11.     Public Property Name As String
    12.     Public Property Age As Integer
    13.  
    14.     Public Property Employment As IEmploymentInfo
    15.         Get
    16.             If _employment Is Nothing Then
    17.                 _employment = New EmploymentInfo
    18.             End If
    19.             Return _employment
    20.         End Get
    21.         Set(value As IEmploymentInfo)
    22.  
    23.         End Set
    24.     End Property
    25.  
    26.     Private Class EmploymentInfo
    27.         Implements IEmploymentInfo
    28.  
    29.  
    30.         Public Sub New()
    31.  
    32.         End Sub
    33.  
    34.         Public Property Address As String Implements IEmploymentInfo.Address
    35.         Public Property CompanyName As String Implements IEmploymentInfo.CompanyName
    36.         Public Property EmployerName As String Implements IEmploymentInfo.EmployerName
    37.     End Class
    38.  
    39. End Class

    The class itself is private inside its parent class but the interface is public so you can access the members outside the class through the interface but without access to the concrete class, one cannot instantiate it outside of its parent.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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