none
Calling parents classes from subclasses RRS feed

  • Question

  • Hello,

    before i start, i want to say that my English is not really good to explain what i am really trying to achieve, but i will try my best.

    Lets say that i have the following classes

    • Account
    • Handler
    • Profile
    • Credentials

    Account class is the main class that contains 4 properties (Handler, Profile, Credentials, and Friends(a profiles array))

    Handler is the class that initial connection, send and receive information form the server and store the information in profile and in friends profiles

    my question is how can i access Friends array from the parent (Account) of the handler?

    Monday, July 23, 2018 1:37 PM

Answers

  • Account is a class so it will always be ByRef regardless of what you specify.  The value of a class is the reference to that class instance, so even though ByVal is the default it works as a reference.  ByVal and ByRef only make a difference on value types (structures, not classes).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Lersubem Wednesday, July 25, 2018 2:18 PM
    Wednesday, July 25, 2018 2:37 AM
    Moderator

All replies

  • Hi

    Please post the actual code. (use the code block tool from the toolbar)


    Regards Les, Livingston, Scotland

    Monday, July 23, 2018 1:51 PM
  • This doesn't sound like you have any actual subclasses - that would indicate that one or more of these classes inherit from some common base class.  These sound like individual classes.  The only relation is that one class instance is exposed through the property of another.

    In that case, you simply ensure that the classes which need to communicate with one another contain references to the external class(es).

    So the instance of Handler should take an instance of Account (the Account instance it is assigned to) and then it can access all of the other properties defined on Account.

    For example:

    Public Class Account
        Public ReadOnly Property Handler As Handler
        Public ReadOnly Property Profile As Profile
        Public ReadOnly Property Friends As New List(Of Profile)
    
        Public Sub New()
            Me.Handler = New Handler(Me)
        End Sub
    End Class
    
    Public Class Handler
        Private _Account As Account
    
        Public Sub New(parentAccount As Account)
            _Account = parentAccount
        End Sub
    
        Public Sub Foo()
            For Each friendProfile In _Account.Friends
                'do something
            Next
        End Sub
    End Class
    
    Public Class Profile
    
    End Class
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 23, 2018 3:01 PM
    Moderator
  • thanks for your answer but i have another question in this case, shall i use ByVal for ByRef in 

    Public Sub New(ByRef? or ByVal? parentAccount As Account)
            _Account = parentAccount
        End Sub
    


    • Edited by Lersubem Tuesday, July 24, 2018 11:59 PM
    Tuesday, July 24, 2018 11:59 PM
  • Account is a class so it will always be ByRef regardless of what you specify.  The value of a class is the reference to that class instance, so even though ByVal is the default it works as a reference.  ByVal and ByRef only make a difference on value types (structures, not classes).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Lersubem Wednesday, July 25, 2018 2:18 PM
    Wednesday, July 25, 2018 2:37 AM
    Moderator
  • thanks for your answer but i have another question in this case, shall i use ByVal for ByRef in 

    Public Sub New(ByRef? or ByVal? parentAccount As Account)
            _Account = parentAccount
        End Sub


    Hey, i have a question, if i have for example 300 profile stored as a list and i did as you said, does that consume memory?
    Saturday, August 4, 2018 3:30 PM
  • thanks for your answer but i have another question in this case, shall i use ByVal for ByRef in 

    Public Sub New(ByRef? or ByVal? parentAccount As Account)
            _Account = parentAccount
        End Sub


    Hey, i have a question, if i have for example 300 profile stored as a list and i did as you said, does that consume memory?

    Could you clarify what you mean by "consume memory"?  Of course every bit of data your application holds consumes some amount of memory.  Those profiles each consume memory based on their size.  The list is really just a collection of pointers to each of those profile objects in memory, so if you are asking if the list consumes more memory than just creating 300 profiles, yes, it does, but not much (its not like there are 300 more copies of the profile data... just more pointers to the one instance of each profile class).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, August 5, 2018 12:30 AM
    Moderator