Results 1 to 5 of 5

Thread: Problems working with tree structured classes....[Resolved]

  1. #1

    Thread Starter
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362

    Problems working with tree structured classes....[Resolved]

    Hello,

    If you have a tree structure of classes, how can you access functions, ect. accross branches of the tree ??

    For example:

    If you have the following classes:

    ClassMain
    Class1
    Class2
    Class3
    ClassA
    ClassB
    ClassC

    And they are structured as two main branches:

    ClassMain.Class1.Class2.Class3
    and
    ClassMain.ClassA.ClassB.ClassC

    How can I easily access Properties or functions of ClassC from inside of Class3:

    VB Code:
    1. '******************
    2. '*** Class Main ***
    3. '******************
    4. Class clsClassMain
    5.     Public ClassA As New clsClassA()
    6.     Public Class1 As New clsClass1()
    7.  
    8.     Public Sub New()
    9.         Debug.WriteLine(ClassA.ClassB.ClassC.DoSomething(123))
    10.         Debug.WriteLine(Class1.Class2.Class3.SomethingNew(456))
    11.     End Sub
    12. End Class
    13.  
    14. '********************
    15. '*** First Branch ***
    16. '********************
    17. Class clsClassA
    18.     Public ClassB As New clsClassB()
    19. End Class
    20.  
    21. Class clsClassB
    22.     Public ClassC As New clsClassC()
    23. End Class
    24.  
    25. Class clsClassC
    26.     Public Function DoSomething(ByVal Value As Int16) As Int16
    27.         'Do something usefull with Value
    28.     End Function
    29. End Class
    30.  
    31. '*********************
    32. '*** Second Branch ***
    33. '*********************
    34. Class clsClass1
    35.     Public Class2 As New clsClass2()
    36. End Class
    37.  
    38. Class clsClass2
    39.     Public Class3 As New clsClass3()
    40. End Class
    41.  
    42. Class clsClass3
    43.     Public Function SomethingNew(ByVal Value As Int16) As Int16
    44.         'ClassMain can't be used here - what can I use to get the reference of the first branch ???
    45.         Return ClassMain.ClassA.ClassB.ClassC.DoSomething(Value) * 16
    46.     End Function
    47. End Class

    I know that I could manually pass object references into each brach, but is there a way to get a "Parent Class" reference ??


    Thanks for any help !!
    Last edited by techman2553; Oct 26th, 2003 at 04:28 PM.
    ----------

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I haven't had to do this yet, but try using modifiers in your class declarations. Such as Friend or Protected.

  3. #3

    Thread Starter
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362
    Thanks, but I have tried every combo of modifiers that would make sense, even forcing everything public doesn't work.

    Any other ideas ???
    ----------

  4. #4

    Thread Starter
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362

    Problems working with tree structured classes.... [Resolved]

    I figured it out:

    This configuration will work if clsClassMain uses the Shared keyword when creating Class1 and ClassA, like this:

    VB Code:
    1. '******************
    2. '*** Class Main ***
    3. '******************
    4. Class clsClassMain
    5.     Public Shared ClassA As New clsClassA()
    6.     Public Shared Class1 As New clsClass1()
    7.  
    8.     Public Sub New()
    9.         Debug.WriteLine(ClassA.ClassB.ClassC.DoSomething(123))
    10.         Debug.WriteLine(Class1.Class2.Class3.SomethingNew(456))
    11.     End Sub
    12. End Class
    13.  
    14. '********************
    15. '*** First Branch ***
    16. '********************
    17. Class clsClassA
    18.     Public ClassB As New clsClassB()
    19. End Class
    20.  
    21. Class clsClassB
    22.     Public ClassC As New clsClassC()
    23. End Class
    24.  
    25. Class clsClassC
    26.     Public Function DoSomething(ByVal Value As Int16) As Int16
    27.         'Do something usefull with Value
    28.     End Function
    29. End Class
    30.  
    31.  
    32. '*********************
    33. '*** Second Branch ***
    34. '*********************
    35. Class clsClass1
    36.     Public Class2 As New clsClass2()
    37. End Class
    38.  
    39. Class clsClass2
    40.     Public Class3 As New clsClass3()
    41. End Class
    42.  
    43. Class clsClass3
    44.  
    45.     Public Function SomethingNew(ByVal Value As Int16) As Int16
    46.         'ClassA and Class1 brances can now be accessed using the original name of ClassMain which is "clsClassMain"
    47.         Return clsClassMain.ClassA.ClassB.ClassC.DoSomething(Value) * 16
    48.     End Function
    49. End Class

    Thanks for the replies !!
    ----------

  5. #5
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    thanks for sharing Techman

    thanks
    nath

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