vb.net 编写程序以使用事件仅打印 6 到 16 之间的偶数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14432162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a program to print only even numbers between 6 and 16 using events
提问by Donna Holtry
I am new to programming as you can see. I need to add an event. We are learning about events and there is really no help and I have tried using the internet for hours. I just want to learn. Thanks ahead of time.
如您所见,我是编程新手。我需要添加一个事件。我们正在了解事件,实际上没有任何帮助,我已经尝试使用互联网几个小时。我只是想学习。提前致谢。
Module Modulel
Public Event PrintThis(ByVal val as Integer)
SubMain()
Dim number as Integer = 6
While number <= 16
// PRINT Goes here .. ??? Not sure if right or code
number = number + 2
End While
回答by Kiran1016
Below link will give you clear understanding of Events. http://www.simple-talk.com/dotnet/.net-framework/custom-events-in-vb.net-2005/.
下面的链接将使您清楚地了解事件。 http://www.simple-talk.com/dotnet/.net-framework/custom-events-in-vb.net-2005/。
Please let me know if it helps.
如果有帮助,请告诉我。
回答by Mark Hall
Something like this should work. You need to use AddHandlerto add the Method that will respond to your event when you call RaiseEvent.
像这样的事情应该有效。您需要使用AddHandler添加将在您调用RaiseEvent时响应您的事件的方法。
Module Module1
Public Event PrintThis(ByVal val As Integer)
Sub Main()
AddHandler PrintThis, AddressOf PrintThisMethod
Dim number As Integer = 6
While number <= 16
RaiseEvent PrintThis(number)
number = number + 2
End While
Console.ReadLine()
End Sub
Private Sub PrintThisMethod(val As Integer)
Console.WriteLine(val)
End Sub
End Module

