Martin Carlsson
Martin Carlsson

Reputation: 3

Access a base class property in inheritance class

I'm using the base class Button in VB.net (VS2017) to create a new class called CDeviceButton. The CDeviceButton then forms as a base for other classes such as CMotorButton, CValveButton.

I want to set the Tag property in the child class CMotorButton but access it in the constructor in CDeviceButton. Doesn't work for me. It turns up being empty.

The Tag is set in the standard property when inserting the CMotorButtom instance into a form.

I've also tried to ensure teh the parent classes' constructors are run by setting mybase.New() as the first action in each constructor but that didn't change anything.

Any ideas for improvements?

Public Class CDeviceButton
    Inherits Button
    Public MMIControl As String = "MMIC"

    Public Sub New()
        MMIControl = "MMIC" & Tag
    End Sub
End class

Public Class CMotorButton
    Inherits CDeviceButton

    Sub New()
        'Do Something
    end Sub
End Class

Upvotes: 0

Views: 1656

Answers (2)

Martin Carlsson
Martin Carlsson

Reputation: 3

Below is my solution I came up with that works. Basically I make sure that all initialization has taken place before reading the Tag property. What I experienced is that the Tag property is empty until the New() in CMotorButton has completed, even though the Tag property has been set when creating the instance of CMotorButton in the Form. TimerInitate has a Tick Time of 500 ms.

Not the most professional solution but works for what I need at the moment.

Another option could be multi threading but that I haven't tried and leave that for future tryouts.

Public Class CDeviceButton
    Inherits Button
    Public MMIControl As String = "MMIC"

    Public Sub New()
        TimerInitiate = New Timer(Me)
    End Sub
    Private Sub TimerInitiate_Tick(sender As Object, e As EventArgs) Handles TimerInitiate.Tick
        If Tag <> Nothing Then
            TimerInitiate.Stop()
            MMIControl = "MMIC" & Tag
        End If 
    End Sub
End class

Public Class CMotorButton
    Inherits CDeviceButton

    Sub New()
        'Do Some stuff
        TimerInitiate.Start()
    End Sub
    Private Sub CMotorButton_Click(sender As Object, e As EventArgs) Handles Me.Click
End Class

Upvotes: 0

Mary
Mary

Reputation: 15091

When you try to concatenate Tag with a string, you are trying to add an object that is probably nothing. I set the Tag property first and used .ToString and it seems to work.

Public Class MyButton
        Inherits Button
        Public Property MyCustomTag As String
        Public Sub New()
            'Using an existing Property of Button
            Tag = "My Message"
            'Using a property you have added to the class
            MyCustomTag = "Message from MyCustomTag property : " & Tag.ToString
        End Sub
    End Class

    Public Class MyInheritedButton
        Inherits MyButton
        Public Sub New()
            If CStr(Tag) = "My Message" Then
                Debug.Print("Accessed Tag property from MyInheritedButton")
                Debug.Print(MyCustomTag)
            End If
        End Sub
    End Class

And then in the Form

Private Sub Test()
    Dim aButton As New MyInheritedButton
    MessageBox.Show(aButton.Tag.ToString)
    MessageBox.Show(aButton.MyCustomTag)
End Sub

Upvotes: 1

Related Questions