Results 1 to 12 of 12

Thread: Nested Classes

  1. #1

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Nested Classes

    I though a nested class could access the parent classes properties and what not, then how come this doesn't work?

    VB Code:
    1. Public Class A
    2.     Public foo As String = "baz"
    3.  
    4.     Public Class B
    5.         Public Sub B()
    6.             foo = "bar"
    7.         End Sub
    8.     End Class
    9.  
    10. End Class

    http://dotgnu.info/pipermail/develop...st/003835.html

    I could have swore I had done this before.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    hmm maybe if you declare it Friend????

    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Nope I tried that but you figure Public would be greater than friend. I also tried MyBase.foo but with no luck.

    FROM THE HELP FILES 'Recommendations on Nested Classes in Components'

    If members of your class need to access private member variables of the object containing it, you can implement it as a nested class. For example, a Wheel object might have a private Radius field. If a Spoke object needs to access the Radius field, it can always have that access if it is implemented as a nested class.

  4. #4
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    I think you still have to have a reference.

    Code:
    Public Class A
        Public foo As String = "baz"
        Public Class B
            Public Sub B()
                Dim A As A
                A.foo = "bar"
            End Sub
        End Class
    End Class

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ohh i think the variable DOES need to be shared.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yeah I guess it does need to be shared, which makes the option less dynamic but I guess you could start an instance of the nested class without the parent class so that makes sense. Oh well, thanks for the quick responses.

    I guess I'll just pass the parent in thru the constructor.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    In C++, if I'm not mistaken, when you access a member variable of a certain class, you are implicitly passing a pointer to that class (the this pointer, similar to me. in .NET). Therefore, in your example, you aren't really accessing foo, you are actually accessing me.foo. This should fail, since the me. reference is to Class B, not Class A. The fully decorated name will work because the reference is correct.

  8. #8

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by Shaggy Hiker
    In C++, if I'm not mistaken, when you access a member variable of a certain class, you are implicitly passing a pointer to that class (the this pointer, similar to me. in .NET). Therefore, in your example, you aren't really accessing foo, you are actually accessing me.foo. This should fail, since the me. reference is to Class B, not Class A. The fully decorated name will work because the reference is correct.
    '

    Whats the 'The fully decorated name'? What I came up with is to pass it in with the constructor since it only worked with Shared properties. I think the problem is that you can make a class B that has no Class A, so in those cases there would be no foo.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Yeah, that's sort of what I meant, though I believe if you define B within A, you can't declare an instance of B outside of A. I believe the compiler would say that B doesn't exist, since the definition is not in scope outside of A.

    By fully decorated name, I meant A.foo as opposed to foo. I think if you just reference foo within B, you are effectively saying me.foo, which translates (roughly) to (this instance of B).foo. Since B.foo doesn't exist, the reference causes a compiler error.

    VB6 was really easy to use. I hope that .NET becomes just as easy, but it could also become just as complex as C++, and then where would we be?

  10. #10

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by Shaggy Hiker
    Yeah, that's sort of what I meant, though I believe if you define B within A, you can't declare an instance of B outside of A. I believe the compiler would say that B doesn't exist, since the definition is not in scope outside of A.
    Unfortunately you can in .NET.

    dim c as New a.b()

    Works fine in this case its use is just for namespace.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    That does seem to violate scoping rules, but it doesn't unsettle me too much. I find it hard to think of a situation where you would want a class defined such that it can only exist within another class.

  12. #12
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Intersting note about nesting

    say you have this situation
    class in a class in a class

    Code:
    Class A
        Class B
            Class C
           
            End Class
        End Class
    
    End Class
    You could do Dim obj As new a.b

    but you could not do Dim obj As New a.b.c

    But if you use a semi colon, you can do as above

    Code:
    Class A
        :
        Class B
            :
            Class C
           
            End Class
        End Class
    
    End Class

    just some fyi
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

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