คำตอบ Raise an event in VB.NET

  • 20 มีนาคม 2555 9:28
     
     

    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


    • แก้ไขโดย Sergiu Dudnic 20 มีนาคม 2555 11:12 edited
    •  

ตอบทั้งหมด

  • 20 มีนาคม 2555 9:40
     
      มีโค้ด

    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

    • เสนอเป็นคำตอบโดย Mark Liu-lxfModerator 22 มีนาคม 2555 2:14
    • ยกเลิกการนำเสนอเป็นคำตอบโดย Mark Liu-lxfModerator 22 มีนาคม 2555 2:15
    •  
  • 20 มีนาคม 2555 10:20
     
      มีโค้ด
    '' 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

  • 20 มีนาคม 2555 11:11
     
     

    attention!

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


    Best regards, Sergiu


    • แก้ไขโดย Sergiu Dudnic 20 มีนาคม 2555 11:13
    •  
  • 20 มีนาคม 2555 13:34
     
      มีโค้ด

    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.

    • แก้ไขโดย pradeep1210MVP 20 มีนาคม 2555 15:00
    •  
  • 20 มีนาคม 2555 14:10
     
     คำตอบ มีโค้ด

    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

    • เสนอเป็นคำตอบโดย Mark Liu-lxfModerator 22 มีนาคม 2555 2:15
    • ทำเครื่องหมายเป็นคำตอบโดย Mark Liu-lxfModerator 30 มีนาคม 2555 7:02
    •  
  • 20 มีนาคม 2555 14:58
     
     คำตอบ มีโค้ด

    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

    • เสนอเป็นคำตอบโดย Mark Liu-lxfModerator 22 มีนาคม 2555 2:15
    • ทำเครื่องหมายเป็นคำตอบโดย Mark Liu-lxfModerator 30 มีนาคม 2555 7:01
    •