6 Replies - 1845 Views - Last Post: 14 July 2011 - 12:47 AM Rate Topic: -----

#1 JizzaDaMan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 23-May 11

Raising events.

Posted 13 July 2011 - 11:47 AM

I have a class with a property, and I want to raise an event when the property value changes, but I have no idea how to declare events, or call them so could someone please enlighten me?
I have read the tutorial using the baby as an example, but it seems so confusing to me.

any help is appreciated, thanks in advance. :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Raising events.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,940
  • Joined: 02-June 10

Re: Raising events.

Posted 13 July 2011 - 12:02 PM

These are in C# so there are some minor language difference but it shouldn't be too tough.

The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,272
  • Joined: 12-June 08

Re: Raising events.

Posted 13 July 2011 - 12:12 PM

The MSDN tutorial on 'raising events' is wonderful!

http://msdn.microsof...v=vs.80%29.aspx

You declare an event like any other object except globally.

<Public/private/protected> Event <name of event>(<zero to n parameters>)



Then in your code you call it like any other method!

Raiseevent <name of event>(<fill in parameters or be empty>)

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,940
  • Joined: 02-June 10

Re: Raising events.

Posted 13 July 2011 - 12:27 PM

I've never liked that direct call of the vent though.
You really should check to see if it is null first.
And make a an event handler for the call to avoid race conditions or Object In Use Elsewhere errors on the rare change that the object you are sending is being used by other classes/methods at the precise time you try to raise the event.

        public void RaiseSomethingTriggered()
        {
            EventHandler handler = SomethingTriggered;
            if (handler != null) handler(this, EventArgs.Empty);
        }


Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Raising events.

Posted 13 July 2011 - 01:14 PM

The eventing system in vb.net is a little more advanced than it is in C#.

Public Sub HandlerForSomeEvent(ByVal sender As Object, e As System.EventArgs) Handles Me.SomeEvent
End Sub

RaiseEvent SomeEvent(Me, New System.EventArgs())



It recommend you use the following style for events.
Public Sub HandlerForSomeEvent(Of T As System.EventArgs)(ByVal sender As Object, e As T) Handles Me.SomeEvent
End Sub




Quote

I've never liked that direct call of the vent though.
You really should check to see if it is null first.

Why not? You can a raise event, even if it has no subscribers.
(Why should you care about who subscribes to you? Are you not the one that insist on decoupling. In particular for GUIs in

A subtle bit is that handlers are not automatically removed, so remember to do RemoveHandler

This post has been edited by AdamSpeight2008: 13 July 2011 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Raising events.

Posted 13 July 2011 - 02:13 PM

Hi,

If all you want is to know when the property value changes, you would be best to have your class implement the INotifyPropertyChanged interface (I use expression trees in my example, of which I explain in the spoiler tag at the bottom of the post).

Your class could look like this:

Public Class YourClass
    Implements INotifyPropertyChanged 'implement interface

    Private _yourField As Boolean = False

    Public Property YourProperty() As Boolean
        Get
            Return Me._yourField
        End Get
        Set(ByVal value As Boolean)
            'if value has changed, then raise the event
            If Not value = Me._yourField Then
                _yourField = value
                Me.onpropertychanged(Of Boolean)(Function() Me.YourProperty)
            End If
        End Set
    End Property

    'implement event required by interface
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    'method used to raise the event
    Protected Overridable Sub onpropertychanged(Of T)(ByVal selector As Expression(Of Func(Of T)))
        'raise event when property changes
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(CType(selector.Body, MemberExpression).Member.Name))
    End Sub

End Class



And I raise and handle the event in Module1, like this:

Imports System.ComponentModel

Module Module1

    Private WithEvents _yourClassInstance As New YourClass()

    Sub Main()
        'subscribe to event
        AddHandler _yourClassInstance.PropertyChanged, AddressOf YourProperty_PropertyChanged
         'change property value which will raise the event
        _yourClassInstance.YourProperty = True
        'unsubscribe from event
        RemoveHandler _yourClassInstance.PropertyChanged, AddressOf YourProperty_PropertyChanged
    End Sub


    'print message when property changes
    Private Sub YourProperty_PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
        Console.WriteLine(String.Format("The property named '{0}' has changed!!!", e.PropertyName))
    End Sub

End Module




If you want to know the old/new value of the property in the event args too, you would be better to create your own event, as in the plentiful links and tutorials provided above, using a very similar template to my code, just without the interface and with your own custom event :)

Spoiler

This post has been edited by CodingSup3rnatur@l-360: 14 July 2011 - 01:23 AM

Was This Post Helpful? 1
  • +
  • -

#7 ranbir  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 04-June 10

Re: Raising events.

Posted 14 July 2011 - 12:47 AM

I do like this;
Public Class MyEventClass
    Public Event ValueChanging(ByVal OldValue As Integer, ByVal NewValue As Integer)

    Private _myValue As Integer
    Public Property MyValue() As Integer
        Get
            Return _myValue
        End Get
        Set(ByVal value As Integer)
            If Not _myValue = value Then RaiseEvent ValueChanging(_myValue, value)
            _myValue = value
        End Set
    End Property    
End Class



and in my form
Public Class Form1
    Dim WithEvents _myClass As New MyEventClass

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        _myClass.MyValue = 2
    End Sub

    Private Sub myvaluechanging(ByVal oldValue As Integer, ByVal newValue As Integer) Handles _myClass.ValueChanging
        MsgBox("Value Changed from " & oldValue & " to " & newValue)
    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1