none
noob question: Can an object expose one of its members functions? RRS feed

  • Question

  • Apologies if this question exposes OOP heretical thinking. I'm a VBA retread trying to wrap my head around all of this stuff. Old dog, new tricks...

    Say I have a Class Automobile. It, in turn, instantiates a Steering class and a Brakes class. Say Automobile wants to make Brakes.Stop directly available as Automobile.Stop and wants to interject its own method Automobile.Steer, and do something on its own before calling Steering.Left or Steering.Right.

    Is there any way for Automobile to expose Brakes.Stop as its own method besides:

    Class Automobile
        Dim myBrakes As New Brakes
        Dim mySteering As New Steering
    
        Sub Stop(howhard as Double) ' doing nothing of value here
            myBrakes.Stop(howhard)
        End Sub
    
        Sub Steer(angle As Single) ' adding value here
            If angle > 0 Then            
                mySteering.SteerRight(angle * SteeringGain)
            ElseIf angle < 0 Then            
                mySteering.SteerLeft(-(angle * SteeringGain))
            End If
        End Sub
    End Class

    Obviously, this is a simplified case, but in some cases if can take lots of typing to add no value to the transition from Automobile.Stop to Brakes.Stop.



    • Edited by Dick Watson Tuesday, November 20, 2018 6:49 AM
    Tuesday, November 20, 2018 6:43 AM

Answers

  • Parroting the member function is, apparently, the solution to this non-real-world problem.
    • Marked as answer by Dick Watson Thursday, November 22, 2018 1:19 AM
    Thursday, November 22, 2018 1:18 AM

All replies

  • https://www.codeproject.com/Articles/8825/Object-Oriented-Programming-In-VB-NET

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/access-levels

    The links should  help explain the basics of OO.

    Tuesday, November 20, 2018 8:53 AM
  • You can expose subs, functions, and objects.

    For example have a look at the Public declaration here:

    Public Class Automobile
        Public myBrakes As New Brakes
        Public mySteering As New Steering
    
        Public Sub Stop(howhard as Double) 
            myBrakes.Stop(howhard)
        End Sub
    
        Public Sub Steer(angle As Single) ' adding value here
            If angle > 0 Then            
                mySteering.SteerRight(angle * SteeringGain)
            ElseIf angle < 0 Then            
                mySteering.SteerLeft(-(angle * SteeringGain))
            End If
        End Sub
    End Class

    Then you can call :

    Dim myCar as new Automobile
    'Assuming Brakes expose Stop as a Public Sub
    myCar.myBrakes.Stop 

    The Brakes are "embedded" inside your Automobile object.

    You can even call the steering directly. It is all a matter of style and code standardization in your Application:

    • What properties are exposed
    • What sub/function are exposed


    So before starting programming you have to ask yourself if your Automobiles will have Brakes and Steering exposed... Maybe it is also a matter of code security.
    Tuesday, November 20, 2018 9:32 AM
  • I've read the second, and articles like the first, several times. They also don't address the situation/question I'm posing.
    Tuesday, November 20, 2018 2:53 PM
  • But my goals were a) to not expose all of Brake's exposures past Automobile, just, for now, its Stop method, and b) to abstract how Automobile.Stop actually works for users of Automobile. Say today Automobile knows Brakes.Stop is good enough to function as Automobile.Stop. At some point in the future Automobile gets another way to slow down in addition to Brakes.Stop and wants to change how it implements Stop. I don't want the users of Automobile to know or care how Automobile's Stop method has changed...

    Why should the code using Automobile have to change when:

    Public Class Automobile
        ...
        Public Sub Stop(howhard as Double) ' not adding value to Brakes' .Stop method
            myBrakes.Stop(howhard)
        End Sub
        ...
    End Class

    becomes:

    Public Class Automobile
        ...
        Public Sub Stop(howHard as Double) ' now adding value
            If howHard < 50 Then
                myEngine.Decelerate(howHard)
            Else
                myEngine.Decelerate(50)
                myBrakes.Stop(howHard - 50)
            End If
        End Sub
        ...
    End Class

    when all the code using Automobile cares about is Automobile.Stop(1000)? I recognize that my original code supports what I'm asking for; just the pass, for now, directly from Automobile.Stop to Brakes.Stop seems like, for now, do nothing code. Is there a way to reduce the amount of do nothing code that has to be written to achieve this kind of goal?



    • Edited by Dick Watson Tuesday, November 20, 2018 3:27 PM
    Tuesday, November 20, 2018 3:18 PM
  • http://www.informit.com/articles/article.aspx?p=170719&seqNum=4

    Tuesday, November 20, 2018 3:48 PM
  • If I understand what you are asking maybe this will help.  BTW - how do you have a method named Stop???

    Class Automobile
    
        <Flags> Public Enum SlowingMethod
            'powers of 2
            brakes = 1
            traction = 2
            fredflinstone = 4 'drag feet
    
            all = brakes Or traction Or fredflinstone
        End Enum
    
        Private myBrakes As New Brakes
        Private mySteering As New Steering
    
        Public Sub SlowDown(SlowMethod As SlowingMethod, howhard As Double) ' doing nothing of value here
            Select Case SlowMethod
                Case SlowingMethod.brakes
                    myBrakes.SlowDown(howhard)
    
                Case SlowingMethod.traction
    
                Case SlowingMethod.fredflinstone
    
                Case SlowingMethod.all
    
            End Select
        End Sub
    
        Public Sub Steer(angle As Single) ' adding value here
            If angle > 0 Then
    
            ElseIf angle < 0 Then
    
            End If
        End Sub
    End Class
    
    Public Class Brakes
    
        Public Sub SlowDown(howhard As Double)
    
        End Sub
    
    End Class
    
    Public Class Steering
    
    End Class


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Tuesday, November 20, 2018 4:20 PM
  • I don't want to seem less than grateful, but... Rather than being a Google for articles, I'd prefer you show how application of that article would suggest I re-write my sample to achieve the objective I'm asking about... If reading articles like that, which I have, helped me solve my problem, I wouldn't have started the thread.

    Inheritance, per se, doesn't work because of a) VB single inheritance says Automobile can't inherit both Engine and Brakes, and b) Automobile is not *just* a greater version Brakes or Engine. In some cases, it passes along about their methods/properties without adding value, in other cases it hides them or uses them itself without letting the code using Automobile know about. Or combines them in new ways that the code using Automobile doesn't need to know or care about.

    Tuesday, November 20, 2018 4:28 PM
  • So I saw another of your posts.  Here is what I would start with.  Notice OutsideAirTemp and braking method.

    Class Automobile
    
        <Flags> Public Enum SlowingMethod
            'powers of 2
            brakes = 1
            traction = 2
            fredflinstone = 4 'drag feet
    
            all = Brakes Or traction Or fredflinstone
        End Enum
    
        Private myBrakes As Brakes
        Private mySteering As Steering
        Private myEngine As Engine
        Public Property OutsideAirTemp As Single
    
        Public Sub New()
            Me.myBrakes = New Brakes
            Me.mySteering = New Steering
            Me.myEngine = New Engine(Me)
        End Sub
    
        Public Sub SlowDown(SlowMethod As SlowingMethod, howhard As Double) ' doing nothing of value here
            Select Case SlowMethod
                Case SlowingMethod.brakes
                    myBrakes.SlowDown(howhard)
    
                Case SlowingMethod.traction
    
                Case SlowingMethod.fredflinstone
    
                Case SlowingMethod.all
    
            End Select
        End Sub
    
        Public Sub Steer(angle As Single) ' adding value here
            If angle > 0 Then
    
            ElseIf angle < 0 Then
    
            End If
        End Sub
    End Class
    
    Class Engine
    
        Private InCar As Automobile
    
        Public Sub New(whAuto As Automobile)
            Me.InCar = whAuto
        End Sub
    
        Private Function OutsideAirTemp() As Single
            Return Me.InCar.OutsideAirTemp
        End Function
    
    End Class
    
    Public Class Brakes
    
        Public Sub SlowDown(howhard As Double)
    
        End Sub
    
    End Class
    
    Public Class Steering
    
    End Class


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Tuesday, November 20, 2018 4:30 PM
  • But the user of Automobile doesn't need to know or care about SlowingMethod. That's kinda half my point. In the present case, users of Automobile only know or care about Stop and Automobile only has one way to do it. In the future case, users of Automobile still only know or care about Stop and now Automobile is trying to hide that it has different SlowingMethod s to achieve Stop.

    I don't understand your question "how do you have a method named Stop?" It's just air code. Are you saying Stop is a reserved word? Or Stop might better be named Slow? Or what?

    Tuesday, November 20, 2018 4:33 PM
  • Multics... Brings back memory of a Honeywell/GlocknerMuller (sp?) embedded controller I used once...

    Anyway, I wish I could award answer points in the other thread for:

    Class Automobile
    
        <Flags> Public Enum SlowingMethod ' now I'm going to have to go find out what this <Flags> thing is all about... Telling the compiler it's a bitmapped enumeration, maybe? Cool...
    ...
        End Enum
    
    ...
        Public Property OutsideAirTemp As Single
    
        Public Sub New()
            ...
            Me.myEngine = New Engine(Me)
        End Sub
    ...
    End Class
    
    Class Engine
    
        Private InCar As Automobile
    
        Public Sub New(whAuto As Automobile)
            Me.InCar = whAuto
        End Sub
    
        Private Function OutsideAirTemp() As Single
            Return Me.InCar.OutsideAirTemp
        End Function
    
    End Class
    

    That seems like a pretty "clean" "OO Good Housekeeping Seal of Approval" way to do this.

    Tuesday, November 20, 2018 4:47 PM
  • I'll be blunt about it. All this Mickey Mouse stuff you at no one uses in the real world.

    Learn how to do half of it what's in the link, and you'll be ok.

    https://www.dofactory.com/net/design-patterns


    Tuesday, November 20, 2018 5:39 PM
  • But the user of Automobile doesn't need to know or care about SlowingMethod. That's kinda half my point. In the present case, users of Automobile only know or care about Stop and Automobile only has one way to do it. In the future case, users of Automobile still only know or care about Stop and now Automobile is trying to hide that it has different SlowingMethod s to achieve Stop.

    I don't understand your question "how do you have a method named Stop?" It's just air code. Are you saying Stop is a reserved word? Or Stop might better be named Slow? Or what?

    Multics seems so long ago.

    I agree that the concepts of direction control, slowing down, speeding up, etc. should be an abstraction.

    Yes, Stop is reserved.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Tuesday, November 20, 2018 6:00 PM
  • Parroting the member function is, apparently, the solution to this non-real-world problem.
    • Marked as answer by Dick Watson Thursday, November 22, 2018 1:19 AM
    Thursday, November 22, 2018 1:18 AM