Let's say I have 2 class declarations like this:
VB Code:
Class ClassA Public SubA End Sub Public VarB as integer Class ClassB End Class End Class
How can I access SubA, or the variable VarA from inside of ClassB?:rolleyes:
Printable View
Let's say I have 2 class declarations like this:
VB Code:
Class ClassA Public SubA End Sub Public VarB as integer Class ClassB End Class End Class
How can I access SubA, or the variable VarA from inside of ClassB?:rolleyes:
eeeh, how?:D I just tried it for the second time and it just works fine.... I dont know why when I first tried it, it wasnt working.....:D:D:D:D
umm, actually, it says "Reference to a non-shared member requires an object reference." umm how can I do that without declaring "varA" in the example above as shared?:confused:
The error message says it all, you need an object reference if you don't want to delcare the sub as shared. So for the example you would do this...
Dim f As New ClassA()
f.SubA()
Which could be added to a sub within classb if you wanted.
Just a quick question, why would you do this?
You can just put the class b outside of class a, and then you will have better luck.
The reason to have a class within a class is so the parent (class A) can instantiate child objects off of the contained class (class B). Child objects should then only comunicate through to the parent by way of events, but the parent can do whatever it wants to to its child objects it created.
Think about it. You create an instance of class A. It then creates an instance to class B and passes a reference of itself to it. Now, class A is referenced twice. If the app's instance of class A goes out of scope, then there is still a reference pointing to A from class B, and class A still has a reference to class B. This is a circular reference. Class A still resides in memory along with class B, but neither can be destroyed because you no longer have a reference to either of them.
At least, this is the way it was explained to me. .Net will find these circular references and destroy them when necessary, but it still isn't good practice from what I was told.
Dear MrPolite, Hi!
Reason of creating a nested class is something more than 'instantiate child objects'. A nested class could access all of the members of its parent class, including Private, Protected and Friend. It also simplifies object model of the project by relating objects together. Somehow, creating a parent class in a nested class is a really bad idea; I think the good approach is passing a reference of parent class to the nested class, like:
Class ClassA
Public SubA
End Sub
Public VarB as integer
Class ClassB
Implements System.IDisposable
Private locOwner as ClassA
Public Sub New(ByVal owner As ClassA)
MyBase.New
locOwner = owner
End Sub
Protected Overridable Overloads Sub Dispose() Implements IDisposable.Dispose
locOwner = Nothing
End Sub
End Class
End Class
So ClassB members have access to all ClassA members like locOwner.SubA or locOwner.VarB = 10. This approach has two weak points:
1: If you have some ClassB Constructors that allow locOwner to be Nothing, you have to validate locOwner every time you want to use it.
2: It's bad to ask a end user call a constructor with a parent argument, So try to simplify that, for example if ClassA is a collection class, provide creating new ClassB members in Add method and so on.
PS: If ClassB inherits from some object that implemented the IDisposale interface, just override the base class Dispose method and call the locOwner = Nothing BEFORE calling the MyBase.Dispose. Implementation codes are no longer needed.
Good luck, MrPolite.