diff --git a/docs/standard/events/how-to-raise-and-consume-events.md b/docs/standard/events/how-to-raise-and-consume-events.md index 940e71fdfe780..d941ad84806ae 100644 --- a/docs/standard/events/how-to-raise-and-consume-events.md +++ b/docs/standard/events/how-to-raise-and-consume-events.md @@ -2,7 +2,6 @@ title: "How to: Raise and Consume Events" description: Raise & consume events in .NET. See examples that use the EventHandler delegate, the EventHandler delegate, & a custom delegate. ms.date: 10/20/2025 -ms.custom: devdivchpfy22 dev_langs: - "csharp" - "vb" @@ -11,31 +10,32 @@ helpviewer_keywords: - "raising events" - "events [.NET], samples" --- -# How to: Raise and Consume Events +# How to: Raise and consume events -The examples in this article show how to work with events. They include examples of the delegate, the delegate, and a custom delegate to illustrate events with and without data. +The examples in this article show how to work with events. They include examples of the delegate, the delegate, and a custom delegate to illustrate events with and without data. The examples use concepts described in the [Events](index.md) article. ## Example 1 - The first example shows how to raise and consume an event that doesn't have data. It contains a class named `Counter` that has an event called `ThresholdReached`. This event is raised when a counter value equals or exceeds a threshold value. The delegate is associated with the event because no event data is provided. +This first example shows how to raise and consume an event that doesn't have data. It contains a class named `Counter` that has an event called `ThresholdReached`. This event is raised when a counter value equals or exceeds a threshold value. The delegate is associated with the event because no event data is provided. - [!code-csharp[EventsOverview#5](../../../samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programnodata.cs#5)] - [!code-vb[EventsOverview#5](../../../samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1nodata.vb#5)] +[!code-csharp[EventsOverview#5](./snippets/raise-consume/csharp/programnodata.cs#5)] +[!code-vb[EventsOverview#5](./snippets/raise-consume/vb/module1nodata.vb#5)] ## Example 2 - The second example shows how to raise and consume an event that provides data. The delegate is associated with the event, and an instance of a custom event data object is provided. - [!code-csharp[EventsOverview#6](../../../samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programwithdata.cs#6)] - [!code-vb[EventsOverview#6](../../../samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb#6)] +This second example shows how to raise and consume an event that provides data. The delegate is associated with the event, and an instance of a custom event data object is provided. + +[!code-csharp[EventsOverview#6](./snippets/raise-consume/csharp/programwithdata.cs#6)] +[!code-vb[EventsOverview#6](./snippets/raise-consume/vb/module1withdata.vb#6)] ## Example 3 - The third example shows how to declare a delegate for an event. The delegate is named `ThresholdReachedEventHandler`. This example is just an illustration. Typically, you don't have to declare a delegate for an event because you can use either the or the delegate. You should declare a delegate only in rare scenarios, such as making your class available to legacy code that can't use generics. +This third example shows how to declare a delegate for an event. The delegate is named `ThresholdReachedEventHandler`. This example is just an illustration. Typically, you don't have to declare a delegate for an event because you can use either the or the delegate. You should declare a delegate only in rare scenarios, such as making your class available to legacy code that can't use generics. - [!code-csharp[EventsOverview#7](../../../samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programwithdelegate.cs#7)] - [!code-vb[EventsOverview#7](../../../samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdelegate.vb#7)] +[!code-csharp[EventsOverview#7](./snippets/raise-consume/csharp/programwithdelegate.cs#7)] +[!code-vb[EventsOverview#7](./snippets/raise-consume/vb/module1withdelegate.vb#7)] ## See also diff --git a/docs/standard/events/index.md b/docs/standard/events/index.md index dd763be6dc2b5..c6b27461b3618 100644 --- a/docs/standard/events/index.md +++ b/docs/standard/events/index.md @@ -15,8 +15,6 @@ helpviewer_keywords: - "events [.NET]" - "events [.NET Core]" - "events [.NET]" -ms.assetid: b6f65241-e0ad-4590-a99f-200ce741bb1f -#customer intent: As a .NET developer, I want to raise and handle .NET events based on the delegate model, so I can enable subscribers to register with or receive notifications from providers. --- # Handle and raise events @@ -33,8 +31,8 @@ Typically, to raise an event, you add a method that is marked as `protected` and The following example shows how to declare an event named `ThresholdReached`. The event is associated with the delegate and raised in a method named `OnThresholdReached`: -[!code-csharp[EventsOverview#1](~/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programtruncated.cs#1)] -[!code-vb[EventsOverview#1](~/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1truncated.vb#1)] +[!code-csharp[EventsOverview#1](./snippets/raise-consume/csharp/programtruncated.cs#1)] +[!code-vb[EventsOverview#1](./snippets/raise-consume/vb/module1truncated.vb#1)] ## Declare delegate signatures for event handlers @@ -48,8 +46,8 @@ Delegates are [multicast](xref:System.MulticastDelegate) class objects, which me Use the and delegate types to define the needed delegate. You mark a delegate with the `delegate` type in [C#](../../csharp/language-reference/builtin-types/reference-types.md#the-delegate-type) or the `Delegate` type in [Visual Basic](../../visual-basic/language-reference/statements/delegate-statement.md) in the declaration. The following example shows how to declare a delegate named `ThresholdReachedEventHandler`: -[!code-csharp[EventsOverview#4](~/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programtruncated.cs#4)] -[!code-vb[EventsOverview#4](~/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1truncated.vb#4)] +[!code-csharp[EventsOverview#4](./snippets/raise-consume/csharp/programtruncated.cs#4)] +[!code-vb[EventsOverview#4](./snippets/raise-consume/vb/module1truncated.vb#4)] ## Work with event data classes @@ -61,8 +59,8 @@ You can create a class that derives from the class to pr The following example shows an event data class named `ThresholdReachedEventArgs` that contains properties that are specific to the event being raised: -[!code-csharp[EventsOverview#3](~/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programtruncated.cs#3)] -[!code-vb[EventsOverview#3](~/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1truncated.vb#3)] +[!code-csharp[EventsOverview#3](./snippets/raise-consume/csharp/programtruncated.cs#3)] +[!code-vb[EventsOverview#3](./snippets/raise-consume/vb/module1truncated.vb#3)] ## Respond to events with handlers @@ -70,8 +68,8 @@ To respond to an event, you define an event handler method in the event receiver The following example shows an event handler method named `c_ThresholdReached` that matches the signature for the delegate. The method subscribes to the `ThresholdReached` event: -[!code-csharp[EventsOverview#2](~/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programtruncated.cs#2)] -[!code-vb[EventsOverview#2](~/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1truncated.vb#2)] +[!code-csharp[EventsOverview#2](./snippets/raise-consume/csharp/programtruncated.cs#2)] +[!code-vb[EventsOverview#2](./snippets/raise-consume/vb/module1truncated.vb#2)] ## Use static and dynamic event handlers diff --git a/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/DesignTimeBuild/.dtbcache.v2 b/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/DesignTimeBuild/.dtbcache.v2 new file mode 100644 index 0000000000000..f3437f8c87d83 Binary files /dev/null and b/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/DesignTimeBuild/.dtbcache.v2 differ diff --git a/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/FileContentIndex/110eb034-4cd0-41ab-b975-3f6c4c4beb0d.vsidx b/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/FileContentIndex/110eb034-4cd0-41ab-b975-3f6c4c4beb0d.vsidx new file mode 100644 index 0000000000000..ac19447a4035b Binary files /dev/null and b/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/FileContentIndex/110eb034-4cd0-41ab-b975-3f6c4c4beb0d.vsidx differ diff --git a/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/v18/.futdcache.v2 b/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/v18/.futdcache.v2 new file mode 100644 index 0000000000000..9fe88a5a33218 Binary files /dev/null and b/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/v18/.futdcache.v2 differ diff --git a/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/v18/.suo b/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/v18/.suo new file mode 100644 index 0000000000000..4ccad37b405f5 Binary files /dev/null and b/docs/standard/events/snippets/raise-consume/csharp/eventsoverview/v18/.suo differ diff --git a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programnodata.cs b/docs/standard/events/snippets/raise-consume/csharp/programnodata.cs similarity index 53% rename from samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programnodata.cs rename to docs/standard/events/snippets/raise-consume/csharp/programnodata.cs index ea92dd0b743c6..4cb743088d32c 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programnodata.cs +++ b/docs/standard/events/snippets/raise-consume/csharp/programnodata.cs @@ -1,13 +1,11 @@ -// -using System; - -namespace ConsoleApplication1 +namespace ConsoleApplication1 { + // class ProgramOne { - static void Main(string[] args) + static void Main() { - Counter c = new Counter(new Random().Next(10)); + Counter c = new(new Random().Next(10)); c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total"); @@ -18,33 +16,28 @@ static void Main(string[] args) } } - static void c_ThresholdReached(object sender, EventArgs e) + static void c_ThresholdReached(object? sender, EventArgs e) { Console.WriteLine("The threshold was reached."); Environment.Exit(0); } } - class Counter + class Counter(int passedThreshold) { - private int threshold; - private int total; - - public Counter(int passedThreshold) - { - threshold = passedThreshold; - } + private readonly int _threshold = passedThreshold; + private int _total; public void Add(int x) { - total += x; - if (total >= threshold) + _total += x; + if (_total >= _threshold) { ThresholdReached?.Invoke(this, EventArgs.Empty); } } - public event EventHandler ThresholdReached; + public event EventHandler? ThresholdReached; } + // } -// diff --git a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programtruncated.cs b/docs/standard/events/snippets/raise-consume/csharp/programtruncated.cs similarity index 64% rename from samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programtruncated.cs rename to docs/standard/events/snippets/raise-consume/csharp/programtruncated.cs index 3507c52fb8a80..4a961ffd27d47 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programtruncated.cs +++ b/docs/standard/events/snippets/raise-consume/csharp/programtruncated.cs @@ -1,6 +1,4 @@ -using System; - -namespace ConsoleApplication2 +namespace ConsoleApplication2 { // class ProgramTwo @@ -10,10 +8,10 @@ static void Main() var c = new Counter(); c.ThresholdReached += c_ThresholdReached; - // provide remaining implementation for the class + // Provide remaining implementation for the class... } - static void c_ThresholdReached(object sender, EventArgs e) + static void c_ThresholdReached(object? sender, EventArgs e) { Console.WriteLine("The threshold was reached."); } @@ -23,14 +21,14 @@ static void c_ThresholdReached(object sender, EventArgs e) // class Counter { - public event EventHandler ThresholdReached; + public event EventHandler? ThresholdReached; protected virtual void OnThresholdReached(EventArgs e) { ThresholdReached?.Invoke(this, e); } - // provide remaining implementation for the class + // Provide remaining implementation for the class... } // @@ -43,6 +41,8 @@ public class ThresholdReachedEventArgs : EventArgs // // - public delegate void ThresholdReachedEventHandler(object sender, ThresholdReachedEventArgs e); + public delegate void ThresholdReachedEventHandler( + object sender, + ThresholdReachedEventArgs e); // } diff --git a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programwithdata.cs b/docs/standard/events/snippets/raise-consume/csharp/programwithdata.cs similarity index 57% rename from samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programwithdata.cs rename to docs/standard/events/snippets/raise-consume/csharp/programwithdata.cs index ce6f3957d1f63..4e0dfcf66c789 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programwithdata.cs +++ b/docs/standard/events/snippets/raise-consume/csharp/programwithdata.cs @@ -1,13 +1,11 @@ -// -using System; - -namespace ConsoleApplication3 +namespace ConsoleApplication3 { + // class ProgramThree { - static void Main(string[] args) + static void Main() { - Counter c = new Counter(new Random().Next(10)); + Counter c = new(new Random().Next(10)); c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total"); @@ -18,30 +16,25 @@ static void Main(string[] args) } } - static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e) + static void c_ThresholdReached(object? sender, ThresholdReachedEventArgs e) { Console.WriteLine($"The threshold of {e.Threshold} was reached at {e.TimeReached}."); Environment.Exit(0); } } - class Counter + class Counter(int passedThreshold) { - private int threshold; - private int total; - - public Counter(int passedThreshold) - { - threshold = passedThreshold; - } + private readonly int _threshold = passedThreshold; + private int _total; public void Add(int x) { - total += x; - if (total >= threshold) + _total += x; + if (_total >= _threshold) { ThresholdReachedEventArgs args = new ThresholdReachedEventArgs(); - args.Threshold = threshold; + args.Threshold = _threshold; args.TimeReached = DateTime.Now; OnThresholdReached(args); } @@ -49,14 +42,10 @@ public void Add(int x) protected virtual void OnThresholdReached(ThresholdReachedEventArgs e) { - EventHandler handler = ThresholdReached; - if (handler != null) - { - handler(this, e); - } + ThresholdReached?.Invoke(this, e); } - public event EventHandler ThresholdReached; + public event EventHandler? ThresholdReached; } public class ThresholdReachedEventArgs : EventArgs @@ -64,5 +53,5 @@ public class ThresholdReachedEventArgs : EventArgs public int Threshold { get; set; } public DateTime TimeReached { get; set; } } + // } -// diff --git a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programwithdelegate.cs b/docs/standard/events/snippets/raise-consume/csharp/programwithdelegate.cs similarity index 61% rename from samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programwithdelegate.cs rename to docs/standard/events/snippets/raise-consume/csharp/programwithdelegate.cs index a9294a3f7aff1..c83c2038b38d4 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/programwithdelegate.cs +++ b/docs/standard/events/snippets/raise-consume/csharp/programwithdelegate.cs @@ -1,13 +1,11 @@ -// -using System; - -namespace ConsoleApplication4 +namespace ConsoleApplication4 { + // class ProgramFour { - static void Main(string[] args) + static void Main() { - Counter c = new Counter(new Random().Next(10)); + Counter c = new(new Random().Next(10)); c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total"); @@ -27,21 +25,21 @@ static void c_ThresholdReached(Object sender, ThresholdReachedEventArgs e) class Counter { - private int threshold; - private int total; + private readonly int _threshold; + private int _total; public Counter(int passedThreshold) { - threshold = passedThreshold; + _threshold = passedThreshold; } public void Add(int x) { - total += x; - if (total >= threshold) + _total += x; + if (_total >= _threshold) { - ThresholdReachedEventArgs args = new ThresholdReachedEventArgs(); - args.Threshold = threshold; + ThresholdReachedEventArgs args = new(); + args.Threshold = _threshold; args.TimeReached = DateTime.Now; OnThresholdReached(args); } @@ -49,11 +47,7 @@ public void Add(int x) protected virtual void OnThresholdReached(ThresholdReachedEventArgs e) { - ThresholdReachedEventHandler handler = ThresholdReached; - if (handler != null) - { - handler(this, e); - } + ThresholdReached?.Invoke(this, e); } public event ThresholdReachedEventHandler ThresholdReached; @@ -65,6 +59,8 @@ public class ThresholdReachedEventArgs : EventArgs public DateTime TimeReached { get; set; } } - public delegate void ThresholdReachedEventHandler(Object sender, ThresholdReachedEventArgs e); + public delegate void ThresholdReachedEventHandler( + object sender, + ThresholdReachedEventArgs e); + // } -// diff --git a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/eventsoverview.csproj b/docs/standard/events/snippets/raise-consume/csharp/project.csproj similarity index 84% rename from samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/eventsoverview.csproj rename to docs/standard/events/snippets/raise-consume/csharp/project.csproj index 6d0d592a5ff2f..a5314e08a612e 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR/eventsoverview/cs/eventsoverview.csproj +++ b/docs/standard/events/snippets/raise-consume/csharp/project.csproj @@ -1,7 +1,7 @@ - net8.0 + net10.0 enable true Exe diff --git a/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1nodata.vb b/docs/standard/events/snippets/raise-consume/vb/module1nodata.vb similarity index 69% rename from samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1nodata.vb rename to docs/standard/events/snippets/raise-consume/vb/module1nodata.vb index 1fb6789349f4c..b99edef2ca809 100644 --- a/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1nodata.vb +++ b/docs/standard/events/snippets/raise-consume/vb/module1nodata.vb @@ -1,8 +1,8 @@ -' +' Module Module1 Sub Main() - Dim c As Counter = New Counter(New Random().Next(10)) + Dim c As New Counter(New Random().Next(10)) AddHandler c.ThresholdReached, AddressOf c_ThresholdReached Console.WriteLine("press 'a' key to increase total") @@ -19,17 +19,17 @@ Module Module1 End Module Class Counter - Private threshold As Integer - Private total As Integer + Private ReadOnly _threshold As Integer + Private _total As Integer Public Sub New(passedThreshold As Integer) - threshold = passedThreshold + _threshold = passedThreshold End Sub Public Sub Add(x As Integer) - total = total + x - If (total >= threshold) Then - ThresholdReached?.Invoke(this. EventArgs.Empty) + _total += x + If (_total >= _threshold) Then + RaiseEvent ThresholdReached(Me, EventArgs.Empty) End If End Sub diff --git a/docs/standard/events/snippets/raise-consume/vb/module1truncated.vb b/docs/standard/events/snippets/raise-consume/vb/module1truncated.vb new file mode 100644 index 0000000000000..d4111b9173091 --- /dev/null +++ b/docs/standard/events/snippets/raise-consume/vb/module1truncated.vb @@ -0,0 +1,42 @@ +Namespace Module2Example + ' + Module Module2 + + Sub Main() + Dim c As New Counter() + AddHandler c.ThresholdReached, AddressOf c_ThresholdReached + + ' provide remaining implementation for the class + End Sub + + Sub c_ThresholdReached(sender As Object, e As EventArgs) + Console.WriteLine("The threshold was reached.") + End Sub + End Module + ' + + ' + Public Class Counter + Public Event ThresholdReached As EventHandler + + Protected Overridable Sub OnThresholdReached(e As EventArgs) + RaiseEvent ThresholdReached(Me, e) + End Sub + + ' provide remaining implementation for the class + End Class + ' + + ' + Public Class ThresholdReachedEventArgs + Inherits EventArgs + + Public Property Threshold As Integer + Public Property TimeReached As Date + End Class + ' + + ' + Public Delegate Sub ThresholdReachedEventHandler(sender As Object, e As ThresholdReachedEventArgs) + ' +End Namespace diff --git a/docs/standard/events/snippets/raise-consume/vb/module1withdata.vb b/docs/standard/events/snippets/raise-consume/vb/module1withdata.vb new file mode 100644 index 0000000000000..126b367ddc8fe --- /dev/null +++ b/docs/standard/events/snippets/raise-consume/vb/module1withdata.vb @@ -0,0 +1,56 @@ +Namespace Module3Example + ' + Module Module3 + + Sub Main() + Dim c As New Counter(New Random().Next(10)) + AddHandler c.ThresholdReached, AddressOf c_ThresholdReached + + Console.WriteLine("press 'a' key to increase total") + While Console.ReadKey(True).KeyChar = "a" + Console.WriteLine("adding one") + c.Add(1) + End While + End Sub + + Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs) + Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached) + Environment.Exit(0) + End Sub + End Module + + Class Counter + Private ReadOnly _threshold As Integer + Private _total As Integer + + Public Sub New(passedThreshold As Integer) + _threshold = passedThreshold + End Sub + + Public Sub Add(x As Integer) + _total += x + If (_total >= _threshold) Then + Dim args As New ThresholdReachedEventArgs With { + .Threshold = _threshold, + .TimeReached = Date.Now + } + OnThresholdReached(args) + End If + End Sub + + Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs) + RaiseEvent ThresholdReached(Me, e) + End Sub + + Public Event ThresholdReached As EventHandler(Of ThresholdReachedEventArgs) + End Class + + Class ThresholdReachedEventArgs + Inherits EventArgs + + Public Property Threshold As Integer + Public Property TimeReached As Date + End Class + ' +End Namespace + diff --git a/docs/standard/events/snippets/raise-consume/vb/module1withdelegate.vb b/docs/standard/events/snippets/raise-consume/vb/module1withdelegate.vb new file mode 100644 index 0000000000000..440ec3376144b --- /dev/null +++ b/docs/standard/events/snippets/raise-consume/vb/module1withdelegate.vb @@ -0,0 +1,59 @@ + +Namespace Module4Example + ' + Module Module4 + + Sub Main() + Dim c As New Counter(New Random().Next(10)) + AddHandler c.ThresholdReached, AddressOf c_ThresholdReached + + Console.WriteLine("press 'a' key to increase total") + While Console.ReadKey(True).KeyChar = "a" + Console.WriteLine("adding one") + c.Add(1) + End While + End Sub + + Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs) + Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached) + Environment.Exit(0) + End Sub + End Module + + Class Counter + Private ReadOnly _threshold As Integer + Private _total As Integer + + Public Sub New(passedThreshold As Integer) + _threshold = passedThreshold + End Sub + + Public Sub Add(x As Integer) + _total += x + If (_total >= _threshold) Then + Dim args As New ThresholdReachedEventArgs With { + .Threshold = _threshold, + .TimeReached = Date.Now + } + OnThresholdReached(args) + End If + End Sub + + Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs) + RaiseEvent ThresholdReached(Me, e) + End Sub + + Public Event ThresholdReached As ThresholdReachedEventHandler + End Class + + Public Class ThresholdReachedEventArgs + Inherits EventArgs + + Public Property Threshold As Integer + Public Property TimeReached As Date + End Class + + Public Delegate Sub ThresholdReachedEventHandler(sender As Object, e As ThresholdReachedEventArgs) + ' + +End Namespace diff --git a/docs/standard/events/snippets/raise-consume/vb/project.vbproj b/docs/standard/events/snippets/raise-consume/vb/project.vbproj new file mode 100644 index 0000000000000..97b1d2a0dc4e7 --- /dev/null +++ b/docs/standard/events/snippets/raise-consume/vb/project.vbproj @@ -0,0 +1,10 @@ + + + + net10.0 + enable + true + Library + + + diff --git a/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1truncated.vb b/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1truncated.vb deleted file mode 100644 index 0b78397d82f75..0000000000000 --- a/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1truncated.vb +++ /dev/null @@ -1,40 +0,0 @@ -' -Module Module1 - - Sub Main() - Dim c As New Counter() - AddHandler c.ThresholdReached, AddressOf c_ThresholdReached - - ' provide remaining implementation for the class - End Sub - - Sub c_ThresholdReached(sender As Object, e As EventArgs) - Console.WriteLine("The threshold was reached.") - End Sub -End Module -' - -' -Public Class Counter - Public Event ThresholdReached As EventHandler - - Protected Overridable Sub OnThresholdReached(e As EventArgs) - RaiseEvent ThresholdReached(Me, e) - End Sub - - ' provide remaining implementation for the class -End Class -' - -' -Public Class ThresholdReachedEventArgs - Inherits EventArgs - - Public Property Threshold As Integer - Public Property TimeReached As DateTime -End Class -' - -' -Public Delegate Sub ThresholdReachedEventHandler(sender As Object, e As ThresholdReachedEventArgs) -' diff --git a/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb b/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb deleted file mode 100644 index b424a89627c4c..0000000000000 --- a/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdata.vb +++ /dev/null @@ -1,53 +0,0 @@ -' -Module Module1 - - Sub Main() - Dim c As Counter = New Counter(New Random().Next(10)) - AddHandler c.ThresholdReached, AddressOf c_ThresholdReached - - Console.WriteLine("press 'a' key to increase total") - While Console.ReadKey(True).KeyChar = "a" - Console.WriteLine("adding one") - c.Add(1) - End While - End Sub - - Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs) - Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached) - Environment.Exit(0) - End Sub -End Module - -Class Counter - Private threshold As Integer - Private total As Integer - - Public Sub New(passedThreshold As Integer) - threshold = passedThreshold - End Sub - - Public Sub Add(x As Integer) - total = total + x - If (total >= threshold) Then - Dim args As ThresholdReachedEventArgs = New ThresholdReachedEventArgs() - args.Threshold = threshold - args.TimeReached = DateTime.Now - OnThresholdReached(args) - End If - End Sub - - Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs) - RaiseEvent ThresholdReached(Me, e) - End Sub - - Public Event ThresholdReached As EventHandler(Of ThresholdReachedEventArgs) -End Class - -Class ThresholdReachedEventArgs - Inherits EventArgs - - Public Property Threshold As Integer - Public Property TimeReached As DateTime -End Class -' - diff --git a/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdelegate.vb b/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdelegate.vb deleted file mode 100644 index 0652accefaacb..0000000000000 --- a/samples/snippets/visualbasic/VS_Snippets_CLR/eventsoverview/vb/module1withdelegate.vb +++ /dev/null @@ -1,55 +0,0 @@ -' -Module Module1 - - Sub Main() - Dim c As Counter = New Counter(New Random().Next(10)) - AddHandler c.ThresholdReached, AddressOf c_ThresholdReached - - Console.WriteLine("press 'a' key to increase total") - While Console.ReadKey(True).KeyChar = "a" - Console.WriteLine("adding one") - c.Add(1) - End While - End Sub - - Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs) - Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached) - Environment.Exit(0) - End Sub -End Module - -Class Counter - Private threshold As Integer - Private total As Integer - - Public Sub New(passedThreshold As Integer) - threshold = passedThreshold - End Sub - - Public Sub Add(x As Integer) - total = total + x - If (total >= threshold) Then - Dim args As ThresholdReachedEventArgs = New ThresholdReachedEventArgs() - args.Threshold = threshold - args.TimeReached = DateTime.Now - OnThresholdReached(args) - End If - End Sub - - Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs) - RaiseEvent ThresholdReached(Me, e) - End Sub - - Public Event ThresholdReached As ThresholdReachedEventHandler -End Class - -Public Class ThresholdReachedEventArgs - Inherits EventArgs - - Public Property Threshold As Integer - Public Property TimeReached As DateTime -End Class - -Public Delegate Sub ThresholdReachedEventHandler(sender As Object, e As ThresholdReachedEventArgs) -' -