|
-
Oct 16th, 2002, 12:40 PM
#1
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:
Public Class A
Public foo As String = "baz"
Public Class B
Public Sub B()
foo = "bar"
End Sub
End Class
End Class
http://dotgnu.info/pipermail/develop...st/003835.html
I could have swore I had done this before.
-
Oct 16th, 2002, 12:44 PM
#2
hmm maybe if you declare it Friend????
-
Oct 16th, 2002, 12:52 PM
#3
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.
-
Oct 16th, 2002, 01:09 PM
#4
Addicted Member
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
-
Oct 16th, 2002, 01:14 PM
#5
ohh i think the variable DOES need to be shared.
-
Oct 16th, 2002, 01:17 PM
#6
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.
-
Oct 17th, 2002, 05:41 PM
#7
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.
-
Oct 17th, 2002, 06:15 PM
#8
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.
-
Oct 23rd, 2002, 12:52 PM
#9
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?
-
Oct 23rd, 2002, 01:15 PM
#10
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.
-
Oct 23rd, 2002, 02:55 PM
#11
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.
-
Oct 23rd, 2002, 03:17 PM
#12
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
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
|