Base Class Events

Ok, so I have a base class that declares an event StatusTextChanged. My child class, of course, cannot directly raise this event.

So, I am completing something like this (for simplicity):

Public MustInherit Class FooBase
    Public Event StatusTextChanged(ByVal StatusText As String)
    Protected Sub RaiseStatusTextChangedEvent(ByVal StatusText As String)
        RaiseEvent StatusTextChanged(StatusText)
    End Sub
End Class

And then in the child class I call MyBase.RaiseStatusTextChangedEvent("something"). Is there a better or more recommended way to do this?

edit: VB.NET or C #, in any case, it works almost the same.

edit: So, after the answers, I am in this in the base class and then just setting the StatusText property in the child class ...

    Public Event StatusTextChanged(ByVal StatusText As String)
    Private _StatusText As String = "Idle."
    Public Property StatusText() As String
        Get
            Return _StatusText
        End Get
        Protected Set(ByVal value As String)
            RaiseEvent StatusTextChanged(value)
        End Set
    End Property
+3
source share
5 answers

, (, , , ).

, :

  • StatusTextChanged Protected Overridable
  • Wrap StatusText EventArgs
  • StatusTextChanged Public Event StatusTextChanged As EventHandler(Of YourCustomEventArgs)

:

eventargs:

Public Class TextEventArgs
    Inherits EventArgs

    Private _text As String

    Public Sub New(ByVal text As String)
        _text = text
    End Sub

    Public ReadOnly Property Text() As String
        Get
            Return _text
        End Get
    End Property

End Class

:

Public Event StatusTextChanged As EventHandler(Of TextEventArgs)
Protected Overridable Sub OnStatusTextChanged(ByVal e As TextEventArgs)
    RaiseEvent StatusTextChanged(Me, e)
End Sub

... , , ; , , :

OnStatusTextChanged(New TextEventArgs("some text"))

, .NET.

+6

, , .

+1

.

, StatusText . FooBase StatusText, accessors, , StatusTextChanged. Setter StatusText. , , .

, StatusText , , , .

0

, , - Status , ( #, VB Visual Studio )

private string _status;

public string Status
{
    get { return _status; }
    protected set
    {
        if (_status == value) return;
        _status = value;
        StatusChanged(value);
    }
}
0

Public Event Status As StatusEventHandler

Protected Overridable Sub OnStatus(ByVal e As StatusEventArgs)
    RaiseEvent Status(Me, e)
End Sub

Public Delegate Sub StatusEventHandler(ByVal sender As Object, _
   ByVal e As StatusEventArgs)

<System.Serializable()> _
Public Class StatusEventArgs
    Inherits System.EventArgs

    Public Sub New()
    End Sub

    Public Sub New(ByVal statusText As String)
        _StatusText = statusText
    End Sub

    'Enter code here for event properties, etc.

    Private _StatusText As String = ""
    Public Property StatusText () As String
        Get
            Return _StatusText
        End get
        Set (ByVal value As String)
            _StatusText = value
        End set
    End property


End class
0
source

Source: https://habr.com/ru/post/1708661/


All Articles