RaiseEvent ... vs直接调用Sub? [英] RaiseEvent... vs Calling Sub directly?

查看:56
本文介绍了RaiseEvent ... vs直接调用Sub?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,


我试图理解提升事件的理性

而不是仅仅调用一个子。有人可以解释以下两种情况之间的

差异吗?为什么我要
想要举办活动而不只是打电话给子?


场景1 - 使用活动

- -------------------------------------------------- -

公共类Form1


Private WithEvents c1 as clsTest


子测试()

c1 =新clsTest()

c1.TestSub()

结束子


Sub cTest(ByVal Msg作为String)处理c1.TestEvent

MsgBox消息

结束子

结束类

----- ------------------------------------------------ <无线电通信/>
公共类clsTest

公共事件TestEvent(ByVal Msg As String)


Public Sub TestSub

Dim strTemp As String =" testing"

RaiseEvent TestEvent(strTemp)

End Sub

End Class


********************************************* *** ***

************************************ ************** ***


场景2 - 没有活动

-------------------------------------- ---------------

Public Class Form1

Dim c1 as clsTest


子测试()

c1 =新clsTest()

c1.TestSub

结束子

结束类

------------------------------------------- ----------

Public Class clsTest

Public Sub TestSub

Test2Sub()

结束Sub


Private Sub Test2Sub()

MsgBox" Testing"

End Sub

结束班

--------------------------------------- ------------


谢谢,

Ron

解决方案



2005年1月6日
通过使用事件,多个方法或组件可以处理

同样的活动....


Public Class Form1


Private WithEvents c1 as clsTest


子测试()

c1 =新clsTest()

c1.TestSub()

End Sub


Sub cTest(ByVal Msg As String)处理c1.TestEvent

MsgBox Msg

End Sub


Sub SECONDTEST(byval msg as string)处理c1.testevent

再做一些工作这里........

结束分

结束课


所以如果不止一个人有实例然后所有这些(在
单独的计算机上,例如.Net Remoting)可以在他们的

计算机上显示消息。它有点像一个新闻通讯,多个人在同一个活动中注册

(发布新版本)。总而言之,如果其他

组件正在使用您的类,可能还想处理该事件,那么您应该使用事件。如果这个回复对您有所帮助,那么请点击

是这条消息上方的方框!我希望这个

有助于清理一些细节!

Joseph MCAD

" Ron"写道:

问候,

我试图理解提升事件的理性而不仅仅是调用子。有人可以解释以下两种情况之间的区别吗?为什么我要提出一个活动而不是仅仅召集一个活动?

Scenario1 - 使用活动
-------------- ----------------------------------------
Public Class Form1
Private WithEvents c1 as clsTest

子测试()
c1 =新clsTest()
c1.TestSub()
End Sub
Sub cTest(ByVal Msg As String)处理c1.TestEvent
MsgBox Msg
End Sub
End Class
----------- ------------------------------------------
Public Class clsTest <公共事件TestEvent(ByVal Msg As String)

Public Sub TestSub
Dim strTemp As String =" testing"
RaiseEvent TestEvent(strTemp)
结束子课程结束课程

*********************************** *************** ***
**************************** ********************** *** *** />
情景2 - 无事件
-------- ---------------------------------------------
公开Class Form1
Di m c1 as clsTest

子测试()
c1 =新clsTest()
c1.TestSub
End Sub
End Class
- -------------------------------------------------- -
Public Class clsTest
Public Sub TestSub
Test2Sub()
End Sub

私有Sub Test2Sub()
MsgBox测试 ;
End Sub
End Class
--------------------------------- ------------------

谢谢,
Ron



< blockquote>嗨Joseph,


感谢您的解释。是的,它为我确定了清楚的事情

。但我没有看到是消息上面的框为

我点击。


问题:您的解释听起来类似于使用

代表。请问这里是否有相似之处?如果

是的,使用

活动或代表之间的细微差别是什么?


感谢您的帮助,

Ron

-----原始消息-----

2005年1月6日

通过使用事件,多个方法或
组件可以处理相同的事件....

Public Class Form1

Private WithEvents c1 as clsTest

子测试()
c1 =新clsTest()
c1.TestSub()
结束子

子cTest( ByVal Msg As String)处理c1.TestEvent
MsgBox消息
End Sub

SUB SECONDTEST(byval msg as string)处理c1.testevent
在这里再做一些工作........
结束子


如果不止一个人拥有c1的实例,那么所有人都需要
(非常计算机)例如.Net Remoting)可以在他们的计算机上显示
的消息。它有点像新闻通讯,其中多个
的人签约同一个活动(发布新版本)。因此,在
结论中,如果其他组件正在使用您的类,可能还希望
处理该事件,那么您应该使用事件。如果这个回复对你有帮助,那么请点击是,然后点击是。这条消息上方的方框!我希望这有助于澄清一些细节!


Joseph MCAD

" Ron"写道:

问候,

我试图理解提升
事件的理性,而不仅仅是调用子。有人可以解释
以下两种情况之间的区别吗?为什么
我想提出一个事件而不是只是调用sub?

Scenario1 - 使用事件
-------------- ----------------------------------------
Public Class Form1
Private WithEvents c1 as clsTest

子测试()
c1 =新clsTest()
c1.TestSub()
End Sub
Sub cTest(ByVal Msg As String)处理c1.TestEvent
MsgBox Msg
End Sub
End Class
----------- ------------------------------------------
Public Class clsTest <公共事件TestEvent(ByVal Msg As String)

Public Sub TestSub
Dim strTemp As String =" testing"
RaiseEvent TestEvent(strTemp)
结束子课程结束课程

*********************************** *************** ***
**************************** ********************** *** *** />
情景2 - 无事件
-------- ---------------------------------------------
公开Class Form1
Dim c1 as clsTest

子测试()
c1 =新clsTest()
c1.TestSub
End Sub
End Class
- -------------------------------------------------- -
Public Class clsTest
Public Sub TestSub
Test2Sub()
End Sub
私有Sub Test2Sub()
MsgBox"测试"
End Sub
End Class
---------------------------------- -----------------

谢谢,
Ron




另一个问题是知道调用哪个子。让我们说你是一个按钮,你需要模仿raisin点击事件,所以有人知道你只是点击了b $ b。你会怎么做?你不知道你在用什么对象 -

表格,用户控件?你不知道开发人员想要采用什么方法

要求通知按钮被点击。


实现一个是不可能的按钮点击事件只需调用

已知方法。


" Ron" <一个******* @ discussions.microsoft.com>在消息中写道

news:04 **************************** @ phx.gbl ... < blockquote class =post_quotes>问候,

我试图理解提升事件的理性而不仅仅是调用子。有人可以解释以下两种情况之间的区别吗?为什么我要提出一个活动而不是仅仅召集一个活动?

Scenario1 - 使用活动
-------------- ----------------------------------------
Public Class Form1
Private WithEvents c1 as clsTest

子测试()
c1 =新clsTest()
c1.TestSub()
End Sub
Sub cTest(ByVal Msg As String)处理c1.TestEvent
MsgBox Msg
End Sub
End Class
----------- ------------------------------------------
Public Class clsTest <公共事件TestEvent(ByVal Msg As String)

Public Sub TestSub
Dim strTemp As String =" testing"
RaiseEvent TestEvent(strTemp)
结束子课程结束课程

*********************************** *************** ***
**************************** ********************** *** *** />
情景2 - 无事件
-------- ---------------------------------------------
公开Class Form1
Di m c1 as clsTest

子测试()
c1 =新clsTest()
c1.TestSub
End Sub
End Class
- -------------------------------------------------- -
Public Class clsTest
Public Sub TestSub
Test2Sub()
End Sub

私有Sub Test2Sub()
MsgBox测试 ;
End Sub
End Class
--------------------------------- ------------------

谢谢,
Ron



Greetings,

I am trying to understand the rational for Raising Events
instead of just calling a sub. Could someone explain the
difference between the following 2 scenarios? Why would I
want to raise an event instead of just calling the sub?

Scenario1 -- Using Events
------------------------------------------------------
Public Class Form1

Private WithEvents c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub()
End Sub

Sub cTest(ByVal Msg As String) Handles c1.TestEvent
MsgBox Msg
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Event TestEvent(ByVal Msg As String)

Public Sub TestSub
Dim strTemp As String = "testing"
RaiseEvent TestEvent(strTemp)
End Sub
End Class

************************************************** ***
************************************************** ***

Scenario2 -- No Events
-----------------------------------------------------
Public Class Form1
Dim c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Sub TestSub
Test2Sub()
End Sub

Private Sub Test2Sub()
MsgBox "Testing"
End Sub
End Class
---------------------------------------------------

Thanks,
Ron

解决方案


Jan. 6, 2005

By using events, more than one method or component can handle the
same event....

Public Class Form1

Private WithEvents c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub()
End Sub

Sub cTest(ByVal Msg As String) Handles c1.TestEvent
MsgBox Msg
End Sub

Sub SECONDTEST(byval msg as string) handles c1.testevent
Do some more work here........
end sub
End Class

So if more than one person had the instance of c1 then all of them (on
seperate computers such as .Net Remoting) could display the message on their
computers. It is sort of like a newsletter where multiple people sign up for
the same event (publishing a new edition). So in conclusion, if other
components are using your class that might want to also handle the event,
then you should use events. If this reply helped you, then please click on
the "Yes" box above this message! I hope this
helps to clear up some of the details!
Joseph MCAD
"Ron" wrote:

Greetings,

I am trying to understand the rational for Raising Events
instead of just calling a sub. Could someone explain the
difference between the following 2 scenarios? Why would I
want to raise an event instead of just calling the sub?

Scenario1 -- Using Events
------------------------------------------------------
Public Class Form1

Private WithEvents c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub()
End Sub

Sub cTest(ByVal Msg As String) Handles c1.TestEvent
MsgBox Msg
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Event TestEvent(ByVal Msg As String)

Public Sub TestSub
Dim strTemp As String = "testing"
RaiseEvent TestEvent(strTemp)
End Sub
End Class

************************************************** ***
************************************************** ***

Scenario2 -- No Events
-----------------------------------------------------
Public Class Form1
Dim c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Sub TestSub
Test2Sub()
End Sub

Private Sub Test2Sub()
MsgBox "Testing"
End Sub
End Class
---------------------------------------------------

Thanks,
Ron



Hi Joseph,

Thanks for your explanation. Yes, it did clear things up
for me. But I don''t see a "Yes" box above the message for
me to click on.

Question: Your explanation sounds similar to using
Delegates. May I ask if there is a similarity here? If
yes, what would be the subtle difference between using
Events or Delegates?

Thanks for your help,
Ron

-----Original Message-----

Jan. 6, 2005

By using events, more than one method or component can handle thesame event....

Public Class Form1

Private WithEvents c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub()
End Sub

Sub cTest(ByVal Msg As String) Handles c1.TestEvent
MsgBox Msg
End Sub

Sub SECONDTEST(byval msg as string) handles c1.testevent
Do some more work here........
end sub
End Class

So if more than one person had the instance of c1 then all of them (onseperate computers such as .Net Remoting) could display the message on theircomputers. It is sort of like a newsletter where multiple people sign up forthe same event (publishing a new edition). So in conclusion, if othercomponents are using your class that might want to also handle the event,then you should use events. If this reply helped you, then please click onthe "Yes" box above this message! I hope this
helps to clear up some of the details!

Joseph MCAD
"Ron" wrote:

Greetings,

I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to raise an event instead of just calling the sub?

Scenario1 -- Using Events
------------------------------------------------------
Public Class Form1

Private WithEvents c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub()
End Sub

Sub cTest(ByVal Msg As String) Handles c1.TestEvent
MsgBox Msg
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Event TestEvent(ByVal Msg As String)

Public Sub TestSub
Dim strTemp As String = "testing"
RaiseEvent TestEvent(strTemp)
End Sub
End Class

************************************************** ***
************************************************** ***

Scenario2 -- No Events
-----------------------------------------------------
Public Class Form1
Dim c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Sub TestSub
Test2Sub()
End Sub

Private Sub Test2Sub()
MsgBox "Testing"
End Sub
End Class
---------------------------------------------------

Thanks,
Ron


.



The other issue is knowing which sub to call. Let''s say you are a button and
you need to mimic raisin the click event so someone knows you were just
clicked. How would you do this? You don''t know what object you are on - a
form, a usercontrol? You don''t know what method the developer wants to get
called to be notified that the button was clicked.

It would be impossible to implement a button click event by just calling
known methods.

"Ron" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...

Greetings,

I am trying to understand the rational for Raising Events
instead of just calling a sub. Could someone explain the
difference between the following 2 scenarios? Why would I
want to raise an event instead of just calling the sub?

Scenario1 -- Using Events
------------------------------------------------------
Public Class Form1

Private WithEvents c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub()
End Sub

Sub cTest(ByVal Msg As String) Handles c1.TestEvent
MsgBox Msg
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Event TestEvent(ByVal Msg As String)

Public Sub TestSub
Dim strTemp As String = "testing"
RaiseEvent TestEvent(strTemp)
End Sub
End Class

************************************************** ***
************************************************** ***

Scenario2 -- No Events
-----------------------------------------------------
Public Class Form1
Dim c1 As clsTest

Sub Testing()
c1 = New clsTest()
c1.TestSub
End Sub
End Class
-----------------------------------------------------
Public Class clsTest
Public Sub TestSub
Test2Sub()
End Sub

Private Sub Test2Sub()
MsgBox "Testing"
End Sub
End Class
---------------------------------------------------

Thanks,
Ron



这篇关于RaiseEvent ... vs直接调用Sub?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆