|
-
Oct 16th, 2014, 05:06 PM
#1
Thread Starter
Junior Member
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
-
Oct 16th, 2014, 07:48 PM
#2
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.
-
Oct 17th, 2014, 04:29 AM
#3
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.
-
Oct 17th, 2014, 06:26 AM
#4
Re: Nested Class - Prevent Instantiation of Nested Class
You could use interfaces:-
vbnet Code:
Public Interface IEmploymentInfo
Property Address As String
Property EmployerName As String
Property CompanyName As String
End Interface
Public Class Person
Private _employment As EmploymentInfo
Public Property Name As String
Public Property Age As Integer
Public Property Employment As IEmploymentInfo
Get
If _employment Is Nothing Then
_employment = New EmploymentInfo
End If
Return _employment
End Get
Set(value As IEmploymentInfo)
End Set
End Property
Private Class EmploymentInfo
Implements IEmploymentInfo
Public Sub New()
End Sub
Public Property Address As String Implements IEmploymentInfo.Address
Public Property CompanyName As String Implements IEmploymentInfo.CompanyName
Public Property EmployerName As String Implements IEmploymentInfo.EmployerName
End Class
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|