Access a base class property in inheritance class












0














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









share|improve this question






















  • Base class' constructors are called first, before the child-class' constructor body. You could make the field a method: Public Function GetMMIControl() As String Return "MMIC" & Tag?.ToString() End Function
    – Rango
    Nov 23 '18 at 16:31










  • Thanks, But the problem I am running into is that the Tag property that is derived from the base class Button isn't assigned a value until the constructor New() in my CMotorClass has finished. So it doesn't look like I can pass the Tag value until New() is done. What I did was to have a timer in CDeviceButton that start at the end of New() in CMotorButton. Tag is then available in CDeviceButton at the first tick of the timer (where I then stop the timer). I'll present the code if that is the final solution (doesn't make me that proud but might work for what I need....)
    – Martin Carlsson
    Nov 23 '18 at 22:20
















0














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









share|improve this question






















  • Base class' constructors are called first, before the child-class' constructor body. You could make the field a method: Public Function GetMMIControl() As String Return "MMIC" & Tag?.ToString() End Function
    – Rango
    Nov 23 '18 at 16:31










  • Thanks, But the problem I am running into is that the Tag property that is derived from the base class Button isn't assigned a value until the constructor New() in my CMotorClass has finished. So it doesn't look like I can pass the Tag value until New() is done. What I did was to have a timer in CDeviceButton that start at the end of New() in CMotorButton. Tag is then available in CDeviceButton at the first tick of the timer (where I then stop the timer). I'll present the code if that is the final solution (doesn't make me that proud but might work for what I need....)
    – Martin Carlsson
    Nov 23 '18 at 22:20














0












0








0







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









share|improve this question













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






vb.net inheritance properties






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 15:38









Martin Carlsson

32




32












  • Base class' constructors are called first, before the child-class' constructor body. You could make the field a method: Public Function GetMMIControl() As String Return "MMIC" & Tag?.ToString() End Function
    – Rango
    Nov 23 '18 at 16:31










  • Thanks, But the problem I am running into is that the Tag property that is derived from the base class Button isn't assigned a value until the constructor New() in my CMotorClass has finished. So it doesn't look like I can pass the Tag value until New() is done. What I did was to have a timer in CDeviceButton that start at the end of New() in CMotorButton. Tag is then available in CDeviceButton at the first tick of the timer (where I then stop the timer). I'll present the code if that is the final solution (doesn't make me that proud but might work for what I need....)
    – Martin Carlsson
    Nov 23 '18 at 22:20


















  • Base class' constructors are called first, before the child-class' constructor body. You could make the field a method: Public Function GetMMIControl() As String Return "MMIC" & Tag?.ToString() End Function
    – Rango
    Nov 23 '18 at 16:31










  • Thanks, But the problem I am running into is that the Tag property that is derived from the base class Button isn't assigned a value until the constructor New() in my CMotorClass has finished. So it doesn't look like I can pass the Tag value until New() is done. What I did was to have a timer in CDeviceButton that start at the end of New() in CMotorButton. Tag is then available in CDeviceButton at the first tick of the timer (where I then stop the timer). I'll present the code if that is the final solution (doesn't make me that proud but might work for what I need....)
    – Martin Carlsson
    Nov 23 '18 at 22:20
















Base class' constructors are called first, before the child-class' constructor body. You could make the field a method: Public Function GetMMIControl() As String Return "MMIC" & Tag?.ToString() End Function
– Rango
Nov 23 '18 at 16:31




Base class' constructors are called first, before the child-class' constructor body. You could make the field a method: Public Function GetMMIControl() As String Return "MMIC" & Tag?.ToString() End Function
– Rango
Nov 23 '18 at 16:31












Thanks, But the problem I am running into is that the Tag property that is derived from the base class Button isn't assigned a value until the constructor New() in my CMotorClass has finished. So it doesn't look like I can pass the Tag value until New() is done. What I did was to have a timer in CDeviceButton that start at the end of New() in CMotorButton. Tag is then available in CDeviceButton at the first tick of the timer (where I then stop the timer). I'll present the code if that is the final solution (doesn't make me that proud but might work for what I need....)
– Martin Carlsson
Nov 23 '18 at 22:20




Thanks, But the problem I am running into is that the Tag property that is derived from the base class Button isn't assigned a value until the constructor New() in my CMotorClass has finished. So it doesn't look like I can pass the Tag value until New() is done. What I did was to have a timer in CDeviceButton that start at the end of New() in CMotorButton. Tag is then available in CDeviceButton at the first tick of the timer (where I then stop the timer). I'll present the code if that is the final solution (doesn't make me that proud but might work for what I need....)
– Martin Carlsson
Nov 23 '18 at 22:20












2 Answers
2






active

oldest

votes


















0














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





share|improve this answer





















  • Thanks, Your solution is not really what I'm aiming for. With setting the "Tag = my message" class MyButton the Tag remains at that value. I do not overwrite it in the InheritedButton class since the Tag property there isn't assigned before the New() method comes to an end. In the test() function I then only read the Tag from the MyButton Class, not from the InheritedClass as I was aiming for. See work around in comment on the first proposal.
    – Martin Carlsson
    Nov 23 '18 at 22:23





















0














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





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449467%2faccess-a-base-class-property-in-inheritance-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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





    share|improve this answer





















    • Thanks, Your solution is not really what I'm aiming for. With setting the "Tag = my message" class MyButton the Tag remains at that value. I do not overwrite it in the InheritedButton class since the Tag property there isn't assigned before the New() method comes to an end. In the test() function I then only read the Tag from the MyButton Class, not from the InheritedClass as I was aiming for. See work around in comment on the first proposal.
      – Martin Carlsson
      Nov 23 '18 at 22:23


















    0














    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





    share|improve this answer





















    • Thanks, Your solution is not really what I'm aiming for. With setting the "Tag = my message" class MyButton the Tag remains at that value. I do not overwrite it in the InheritedButton class since the Tag property there isn't assigned before the New() method comes to an end. In the test() function I then only read the Tag from the MyButton Class, not from the InheritedClass as I was aiming for. See work around in comment on the first proposal.
      – Martin Carlsson
      Nov 23 '18 at 22:23
















    0












    0








    0






    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





    share|improve this answer












    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 17:39









    Mary

    3,0342718




    3,0342718












    • Thanks, Your solution is not really what I'm aiming for. With setting the "Tag = my message" class MyButton the Tag remains at that value. I do not overwrite it in the InheritedButton class since the Tag property there isn't assigned before the New() method comes to an end. In the test() function I then only read the Tag from the MyButton Class, not from the InheritedClass as I was aiming for. See work around in comment on the first proposal.
      – Martin Carlsson
      Nov 23 '18 at 22:23




















    • Thanks, Your solution is not really what I'm aiming for. With setting the "Tag = my message" class MyButton the Tag remains at that value. I do not overwrite it in the InheritedButton class since the Tag property there isn't assigned before the New() method comes to an end. In the test() function I then only read the Tag from the MyButton Class, not from the InheritedClass as I was aiming for. See work around in comment on the first proposal.
      – Martin Carlsson
      Nov 23 '18 at 22:23


















    Thanks, Your solution is not really what I'm aiming for. With setting the "Tag = my message" class MyButton the Tag remains at that value. I do not overwrite it in the InheritedButton class since the Tag property there isn't assigned before the New() method comes to an end. In the test() function I then only read the Tag from the MyButton Class, not from the InheritedClass as I was aiming for. See work around in comment on the first proposal.
    – Martin Carlsson
    Nov 23 '18 at 22:23






    Thanks, Your solution is not really what I'm aiming for. With setting the "Tag = my message" class MyButton the Tag remains at that value. I do not overwrite it in the InheritedButton class since the Tag property there isn't assigned before the New() method comes to an end. In the test() function I then only read the Tag from the MyButton Class, not from the InheritedClass as I was aiming for. See work around in comment on the first proposal.
    – Martin Carlsson
    Nov 23 '18 at 22:23















    0














    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





    share|improve this answer


























      0














      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





      share|improve this answer
























        0












        0








        0






        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 9:27









        Martin Carlsson

        32




        32






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449467%2faccess-a-base-class-property-in-inheritance-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)