Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Nested Classes (after much hair pulling)

Nested Classes (after much hair pulling)

Scheduled Pinned Locked Moved Visual Basic
xmlquestionworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ray Cassick
    wrote on last edited by
    #1

    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?

    Richard DeemingR 1 Reply Last reply
    0
    • R Ray Cassick

      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?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Make the nested classes and their properties Public, but make the constructors as Friend. That way, only code within your assembly can create instances of the internal class. You may be better off using Shared 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 Sub

          Public 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups