Results 1 to 9 of 9

Thread: [RESOLVED] Nested classes - duplicate class name but no error

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    48

    Resolved [RESOLVED] Nested classes - duplicate class name but no error

    Hi,

    I have the following (variables made up just to give you an idea of the class layout)

    Code:
    Public Class _Application_Events
    
        Public Class _Flags
        
            Public Var_A as Boolean
    
        End Class
    
        Public Class _Timers
    
            Public Var_B as Boolean
    
        End Class
    
        Public Flags As New _Flags
        Public Timers As New _Timers
    
    End Class
    This way I can create a new object based on the _Application_Events class and access like thus

    obj_App_Events.Flags.Var_A = True ' Works fine
    obj_App_Events.Timers.Var_B = True ' Works fine

    What I want to do is add a 'Flags' class into the _Timer class so it would be like this

    Code:
    Public Class _Application_Events
    
        Public Class _Flags
        
            Public Var_A as Boolean
    
        End Class
    
        Public Class _Timers
    
            Public Var_B as Boolean
    
            Public Sub New
    
                Public Flags As New _Flags
    
            End Sub
    
            Public Class _Flags
                
                Public Var_C as Boolean
    
            End Class
    
        End Class
    
        Public Flags As New _Flags
        Public Timers As New _Timers
    
    End Class
    So I can then access like this

    obj_App_Events.Flags.Var_A = True ' Works fine
    obj_App_Events.Timers.Var_B = True ' Works fine

    obj_App_Events.Timers.Flags.Var_C = True ' Can only see Var_A

    The compiler doesn't throw an error to inform there are 2 classes with the same name.

    Any ideas how I would access Var_C without changing the class name from _Flags?

    Thanks
    Last edited by gjpollitt; Aug 2nd, 2015 at 09:43 AM.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,759

    Re: Nested classes - duplicate class name but no error

    You have a simple syntax issue with the constructor; it has no End Sub.

    Code:
    Public Class _Application_Events
    
        Public Class _Flags
    
            Public Var_A As Boolean
    
        End Class
    
        Public Class _Timers
    
            Public Var_B As Boolean
    
            ' Public Sub New() <-- comment this out
    
            Public Flags As New _Flags
    
            Public Class _Flags
    
                Public Var_C As Boolean
    
            End Class
    
    End Class
    
    Public Flags As New _Flags
    Public Timers As New _Timers
    
    End Class
    Then you can do this...

    Code:
        Sub Main()
            Dim f As New _Application_Events
            f.Timers.Flags.Var_C = False
    
        End Sub
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,441

    Re: Nested classes - duplicate class name but no error

    I realize that this is just an example, and is therefore pretty highly contrived and likely doesn't represent what you really want to do, but as it stands, it is well on the way to being a mess.

    Considering the real world that this example represents, I see only two possibilities:

    1) The Flags class really is the same thing, as is shown, in which case there is no good reason to create a different Flags definition in the Timer class, and no good reason to create a Flags definition in _Application_Events. The Flags class should be defined outside of any other class, and both _Application_Events and _Timers should have an instance variable of type Flags.

    2) The Flags classes aren't really the same object in _Application_Events and _Timers, in which case they shouldn't have the same name.

    I suspect that what is going on is that they aren't really the same name, but that what you are showing is so unusual that you have found a case where one variable is masking the other variable. Defining classes within classes has very little utility in the real world, and making them Public removes what little utility there is. So, I would say that whatever you are actually trying to do, you should reconsider the roles the classes are playing. Specifically, you should be asking yourself what purpose you expect to gain from having classes defined within other classes as opposed to having them all defined at the same level and just use instances as members within the _Application_Events class.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    48

    Re: Nested classes - duplicate class name but no error

    Thanks for the quick reply.

    I hadn't missed off the 'End Sub' in my code only in the example above (which doesn't help I know)
    Have edited it in.

    I quit out of VS2008, relaunched and it works fine now.

    Thanks again
    Last edited by gjpollitt; Aug 2nd, 2015 at 09:44 AM.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,441

    Re: Nested classes - duplicate class name but no error

    I wrote that while Kebo was also posting. Having seen what he posted, I will add this: I saw what he saw, but assumed that this was just an example written freehand, and that the omission wasn't relevant. I was going to say that the inner and outer flags are not actually the same class, which is why the compiler doesn't complain. One is type _Application_Events._Flags, and the other is _Application_Events._Timers._Flags.

    However, I don't think that making this work is a good idea, as I stated in my earlier post. Sometimes, solving the immediate problem isn't the right thing to do.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    48

    Re: Nested classes - duplicate class name but no error

    Quote Originally Posted by Shaggy Hiker View Post
    I realize that this is just an example
    I have two classes both called _Flags but each one will contain different variables. I though of just dumping them all into one class but thought I would try it this way as it looks cool in intellisense!

    I am only a hobby programmer and code applications where only I will ever see or work with the source code.

    Thanks for replying

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    48

    Re: Nested classes - duplicate class name but no error

    Quote Originally Posted by Shaggy Hiker View Post
    One is type _Application_Events._Flags, and the other is _Application_Events._Timers._Flags.
    That's exactly what I want but it would only show the _Flags members and not the _Timers._Flags members until I restarted the IDE.

    Solved now anyway

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,441

    Re: [RESOLVED] Nested classes - duplicate class name but no error

    That makes it sound like the compiler got an internal table messed up, temporarily. That happens, though it's rare.
    My usual boring signature: Nothing

  9. #9
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: [RESOLVED] Nested classes - duplicate class name but no error

    As a side note, I'd be careful with the underscore prefixes as VS does do use those internally (for example with a property declaration). While it's up to you, I wouldn't use underscores this way, and would use a more 'standard' naming nomenclature.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

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