Nested Classes (after much hair pulling)
-
Ok, I want to create an object hierarchy within my application that will take care of holding some common settings as an alternative to using global vars. I also want this to be a holding area for the application level configuration info that will be read in from a few XML configuration files. My intent is to serialize this data to an XML file latter and pass the file onto some external plug in type components so they can parse through it as they need to and grab what ever configuration settings they need. The basic gist of what I am looking for is this.... I want to have a parent class (App) to contain a few nested classes (Paths, Config, etc...), and each of these nested classes to have some properties internal to them. My intent here is to be able to create one instance of the parent class and get access to all the members of the child classes as so:
App.Paths.Path1
...
App.Config.Setting1
...Now, what I wanted (expected to be able to do) was:
Class App
Public Paths as CPaths Public Config as CConfig Public sub New() Paths = new CPaths Config = new CConfig End sub Protected Class CPaths Private \_Path1 as string Protected sub New() ... Protected Readonly Property Path1 as String Get Return \_Path1 End Get End Property End Class Protected Class CConfig Private \_Setting1 as string Protected sub New() ... Protected Readonly Property Setting1 as String Get Return \_Setting1 End Get End Property End Class
End Class
Instead what I HAD to do was make each one of the nested classes public because I could not declare a public instance of a protected nested class in the parent class constructor!!! While this does appear to work, it also seems to allow a developer to bypass my parent class and directly create instances of my nested classes. IE They can instansiate the public members of the class directly like so:
Dim objAppPaths As New App.CPaths
This really seems to be counter productive to building this hierarchy in the first place. Am I missing something here?
-
Ok, I want to create an object hierarchy within my application that will take care of holding some common settings as an alternative to using global vars. I also want this to be a holding area for the application level configuration info that will be read in from a few XML configuration files. My intent is to serialize this data to an XML file latter and pass the file onto some external plug in type components so they can parse through it as they need to and grab what ever configuration settings they need. The basic gist of what I am looking for is this.... I want to have a parent class (App) to contain a few nested classes (Paths, Config, etc...), and each of these nested classes to have some properties internal to them. My intent here is to be able to create one instance of the parent class and get access to all the members of the child classes as so:
App.Paths.Path1
...
App.Config.Setting1
...Now, what I wanted (expected to be able to do) was:
Class App
Public Paths as CPaths Public Config as CConfig Public sub New() Paths = new CPaths Config = new CConfig End sub Protected Class CPaths Private \_Path1 as string Protected sub New() ... Protected Readonly Property Path1 as String Get Return \_Path1 End Get End Property End Class Protected Class CConfig Private \_Setting1 as string Protected sub New() ... Protected Readonly Property Setting1 as String Get Return \_Setting1 End Get End Property End Class
End Class
Instead what I HAD to do was make each one of the nested classes public because I could not declare a public instance of a protected nested class in the parent class constructor!!! While this does appear to work, it also seems to allow a developer to bypass my parent class and directly create instances of my nested classes. IE They can instansiate the public members of the class directly like so:
Dim objAppPaths As New App.CPaths
This really seems to be counter productive to building this hierarchy in the first place. Am I missing something here?
Make the nested classes and their properties
Public
, but make the constructors asFriend
. That way, only code within your assembly can create instances of the internal class. You may be better off usingShared
methods instead, which would remove the need to create instances of the class. For example:Class App
Public Class Paths
Private Shared _Path1 as String
Shared Sub New()
'Initialize your properties here
End Sub
Private Sub New()
'No need to create instances of this class
End SubPublic Shared Readonly Property Path1 As String Get Return \_Path1 End Get End Property End Class Public Class Config Private Shared \_Setting1 As String Shared Sub New() 'Initialize your properties here End Sub Private Sub New() 'No need to create instances of this class End Sub Public Shared Readonly Property Setting1 As String Get Return \_Setting1 End Get End Property End Class Shared Sub New() 'Initialize any properties for App here End Sub Private Sub New() 'No need to create instances of this class End Sub
End Class
Then, you can just use:
Dim Path1 As String = App.Paths.Path1
Updated: the member variables have to be Shared as well! :-O