Answered Raise an event in VB.NET

  • Tuesday, March 20, 2012 9:28 AM
     
     

    I need to transform the following in VB.NET from C# (.NET 4)

    class MyControl

    public event EventHandler ContentScaleChanged;

    if (myControlOther.ContentScaleChanged != null)
    {
        myControlOther.ContentScaleChanged(c, EventArgs.Empty);
    }

    please help


    Best regards, Sergiu


    • Edited by Sergiu Dudnic Tuesday, March 20, 2012 11:12 AM edited
    •  

All Replies

  • Tuesday, March 20, 2012 9:40 AM
     
      Has Code

    Can you show where you need this (show what c in this context is), because mostly raising an event in VB is just

    RaiseEvent(me, EventsArgs.Empty)

    http://msdn.microsoft.com/en-us/library/fwd3bwed(v=vs.100).aspx


    Success
    Cor

  • Tuesday, March 20, 2012 10:20 AM
     
      Has Code
    '' declaration
    Public Event ContentScaleChanged As EventHandler

    '' raising the event
    RaiseEvent ContentScaleChanged(Me, New EventArgs)

    While raising for events, you don't need to check for nothing(null) in VB.

    Pradeep, Microsoft MVP (Visual Basic)
    http://pradeep1210.wordpress.com

  • Tuesday, March 20, 2012 11:11 AM
     
     

    attention!

    the C# code was not ContentScaleChanged(c, EventArgs.Empty), but myControlOther.ContentScaleChanged(c, EventArgs.Empty);


    Best regards, Sergiu


  • Tuesday, March 20, 2012 1:34 PM
     
      Has Code

    attention!

    the C# code was not ContentScaleChanged(c, EventArgs.Empty), but myControlOther.ContentScaleChanged(c, EventArgs.Empty);


    Best regards, Sergiu


    If the event is defined inside the class from where it is being raised, you will directly call

    RaiseEvent EventName(Me, New EventArgs)

    EDIT (statement corrected) :
    If the event is defined in another class, then you can't raise that event. In such cases, define an event inside your class. Then Raise it on appropriate events of that object.

  • Tuesday, March 20, 2012 2:10 PM
     
     Answered Has Code

    Yea if I now understand you right, is this something often done wrong by C# developers. 

    They raise an event inside a class. I don't know whey they always do it because for the same the method can be called direct without the event. 

    If this is not done from an extra thread then simply do.

    ContentscaleChanged(c, EventArgs.Empty)

    and then in C# end this with a semicolon.

    There is no difference in this between VB and C# although I've seen doing this more wrong by C# developers then by VB. (Although they do that often with the click which had to be done AFAIK VB4 like that)


    Success
    Cor

  • Tuesday, March 20, 2012 2:58 PM
     
     Answered Has Code

    Sorry... my bad. I think my head was spinning. :p

    Please read it like this:

    If the event is defined inside the class from where it is being raised, you will directly call

    RaiseEvent EventName(Me, New EventArgs)

    If the event is defined in another class, then you can't raise that event. In such cases, define an event inside your class. Then Raise it on appropriate events of that object.


    Pradeep, Microsoft MVP (Visual Basic)
    http://pradeep1210.wordpress.com