Skip to content

Commit 7ed9193

Browse files
CopilotBillWagner
andcommitted
Fix VB.NET syntax errors in RaiseEvent example - remove C# string interpolation, move to standalone classes, use proper VB syntax
Co-authored-by: BillWagner <[email protected]>
1 parent 36307a1 commit 7ed9193

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

docs/visual-basic/language-reference/statements/raiseevent-statement.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ RaiseEvent eventname[( argumentlist )]
5656

5757
The example demonstrates a simple timer that counts down from 10 to 0 seconds and displays the progress to the console. When the countdown finishes, it displays "Done".
5858

59-
Add a `WithEvents` variable to your class declarations.
59+
Declare a `WithEvents` variable in your class to handle events from the timer:
6060

6161
[!code-vb[VbVbalrEvents#14](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#14)]
6262

samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -157,47 +157,46 @@ Class Class306ff8ed74dd4b6abd2fe91b17474042
157157
End Class
158158
' </snippet13>
159159

160-
Class Form1
161-
Inherits Form
162-
Private WithEvents Button1 As New Button
163-
Private WithEvents TextBox1 As New TextBox
164-
165-
' <snippet14>
160+
' <snippet14>
161+
Public Class TimerExample
166162
Private WithEvents mTimer As TimerState
167163
' </snippet14>
168164

169165
' <snippet15>
170-
Private WithEvents mTimer As TimerState
171-
172-
Private Sub StartCountdownExample()
173-
Console.WriteLine("Starting countdown from 10 seconds...")
166+
Public Sub StartCountdownExample()
174167
mTimer = New TimerState()
175168
mTimer.StartCountdown(10.0, 1.0)
176169
End Sub
177170

178171
Private Sub mTimer_UpdateTime(ByVal Countdown As Double) Handles mTimer.UpdateTime
179-
Console.WriteLine($"Time remaining: {Countdown:0.0} seconds")
172+
Console.WriteLine("Time remaining: " & Format(Countdown, "##0.0") & " seconds")
180173
End Sub
181174

182175
Private Sub mTimer_Finished() Handles mTimer.Finished
183176
Console.WriteLine("Done")
184177
End Sub
178+
End Class
185179

186-
Class TimerState
187-
Public Event UpdateTime(ByVal Countdown As Double)
188-
Public Event Finished()
189-
Public Sub StartCountdown(ByVal Duration As Double,
190-
ByVal Increment As Double)
191-
Dim SoFar As Double = 0
192-
Do While SoFar < Duration
193-
System.Threading.Thread.Sleep(CInt(Increment * 1000)) ' Sleep for increment seconds
194-
SoFar += Increment
195-
RaiseEvent UpdateTime(Duration - SoFar)
196-
Loop
197-
RaiseEvent Finished()
198-
End Sub
199-
End Class
200-
' </snippet15>
180+
Public Class TimerState
181+
Public Event UpdateTime(ByVal Countdown As Double)
182+
Public Event Finished()
183+
Public Sub StartCountdown(ByVal Duration As Double,
184+
ByVal Increment As Double)
185+
Dim SoFar As Double = 0
186+
Do While SoFar < Duration
187+
System.Threading.Thread.Sleep(CInt(Increment * 1000))
188+
SoFar += Increment
189+
RaiseEvent UpdateTime(Duration - SoFar)
190+
Loop
191+
RaiseEvent Finished()
192+
End Sub
193+
End Class
194+
' </snippet15>
195+
196+
Class Form1
197+
Inherits Form
198+
Private WithEvents Button1 As New Button
199+
Private WithEvents TextBox1 As New TextBox
201200

202201
End Class
203202
End Class

0 commit comments

Comments
 (0)