locked
noob question 2: parent object exposing its properties to its members RRS feed

  • Question

  • Along the same ignorant noob lines, a second question... Say Class Automobile has a property for outside air temperature. How can that property be made visible to Automobile's members any way besides passing it on?

    Class Automobile
        Public Property OutsideAirTemp As Single
        Dim myEngine As New Engine
        ...
        ' is there a better way for myEngine to get this?
        myEngine.OutsideAirTemp = Me.OutsideAirTemp
    End Class
    
    Class Engine
        ' Engine needs to know OutsideAirTemp; how to access it?
        ...
        Property OutsideAirTemp As Single
    End Class
    Thanks in advance.

    Tuesday, November 20, 2018 7:00 AM

Answers

  • Public Shared Property seems to be a pretty good solution to the requirement I was addressing here.
    • Marked as answer by Dick Watson Thursday, November 22, 2018 1:13 AM
    Thursday, November 22, 2018 1:13 AM

All replies

  • public Class Automobile
        Public Property OutsideAirTemp As Single
    end class
    
     public Class Engine
        ' Engine needs to know OutsideAirTemp; how to access it?
        ...
        Property OutsideAirTemp As Single
    End Class


    some other code, like in Form_Load

    dim auto - new Automobile()

    auto.OutsideAirTemp = 1000

    dim eng - new Engine()

    eng .OutsideAirTemp = auto.OutsideAirTemp


    • Edited by DA924x Tuesday, November 20, 2018 8:38 AM
    • Proposed as answer by KareninstructorMVP Tuesday, November 20, 2018 11:23 AM
    • Unproposed as answer by Dick Watson Tuesday, November 20, 2018 2:45 PM
    Tuesday, November 20, 2018 8:37 AM
  • You could take this farther e.g.

    Public Class Mazda
        Public Property Miata As Miata
        Public Property CX7() As CX7
    End Class
    Public Enum MiataEdition
        Club
        GrandTouring
    End Enum
    Public Enum CX7Edition
        Standard
        Delux
    End Enum
    Public Class Miata
        Inherits Automobile
    
        Public Property Edition() As MiataEdition
    End Class
    Public Class CX7
        Inherits Automobile
        Public Property Edition() As CX7Edition
    End Class
    Public Class Automobile
        Public Property Year() As Integer
        Public Property Color() As Color
        Public Property OutsideAirTemp As Single
        Property Engine() As Engine
    End Class
    Public Class Engine
        Public Property Temperature() As Integer
        Public Property HorsePower() As Integer
    End Class

    Then

    Dim Mazda As New Mazda
    
    Dim Miata = New Miata With
            {
                .Color = Color.Black,
                .Engine = New Engine() With {.HorsePower = 200},
                .Edition = MiataEdition.GrandTouring
            }
    
    Dim cx7 As New CX7 With
    {
        .Color = Color.Red,
        .Engine = New Engine() With {.HorsePower = 200},
        .Edition = CX7Edition.Delux
    }
    
    
    Mazda.Miata = Miata
    Mazda.CX7 = cx7
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, November 20, 2018 11:22 AM
  • But the problem I have with this is I was hoping to avoid this mythic "other code" that supplies OusideAirTemp to Automobile having to know all the things in Automobile that needed to know OutsideAirTemp. Or to have to change all the code that supplies OutsideAirTemp to Automobile every time some other component object of Automobile is changed to function dependent on knowing OutsideAirTemp. Or, for that matter, to have to change Automobile every time one of its component objects changes to function dependent on knowing OutsideAirTemp that surrounds Automobile. And to avoid the "other code" from having to know anything about the components of Automobile.

    To use (misuse?) two OO terms, I was hoping to use the class Automobile to encapsulate the objects that make up an Automobile, and to abstract what Automobile does with OutsideAirTemp. Let me update my fragment from above to better show the real world problem.

    Class Automobile
        WriteOnly Public Property OutsideAirTemp As Single
            Set (value As Single)
                myEngine.OutsideAirTemp = value
                myClimateControl.OutsideAirTemp = value
                ' ad nauseum; Automobile doesn't even use it
             End Set
        End Property
        Dim myEngine As New Engine
        Dim myClimateControl As New ClimateControl
        ...
    End Class
    
    Class Engine
        ' Engine needs to know OutsideAirTemp; how to access it?
        ...
        Property OutsideAirTemp As Single
    End Class
    
    Class ClimateControl
        ' ClimateControl also needs to know OutsideAirTemp; how to access it?
        ...
        Property OutsideAirTemp As Single
    End Class

    Tuesday, November 20, 2018 2:45 PM
  • Public Shared Property seems to be a pretty good solution to the requirement I was addressing here.
    • Marked as answer by Dick Watson Thursday, November 22, 2018 1:13 AM
    Thursday, November 22, 2018 1:13 AM