Results 1 to 6 of 6

Thread: Classes -> accessing a sub outside the class

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Classes -> accessing a sub outside the class

    Let's say I have 2 class declarations like this:

    VB Code:
    1. Class ClassA
    2.  
    3.       Public SubA
    4.       End Sub
    5.      
    6.       Public VarB as integer
    7.  
    8.       Class ClassB
    9.  
    10.  
    11.       End Class
    12.  
    13. End Class

    How can I access SubA, or the variable VarA from inside of ClassB?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    eeeh, how? 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.....
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Member
    Join Date
    Jul 2002
    Posts
    49
    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.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  6. #6
    Member Vahid's Avatar
    Join Date
    Aug 2002
    Location
    Iran
    Posts
    37

    Wink

    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.

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