Skip to content

Commit bafe8e7

Browse files
CopilotBillWagner
andcommitted
Move large inline code example to snippet file and use proper reference
Co-authored-by: BillWagner <[email protected]>
1 parent 57bd2e4 commit bafe8e7

File tree

2 files changed

+2
-85
lines changed

2 files changed

+2
-85
lines changed

docs/visual-basic/misc/bc42328.md

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -145,90 +145,7 @@ End Sub
145145

146146
The following full example demonstrates the warning and multiple solutions:
147147

148-
```vb
149-
Imports System
150-
151-
Public Class DocumentProcessor
152-
' Standard .NET event using EventHandler
153-
Public Event DocumentProcessed As EventHandler
154-
155-
' Custom event with different signature
156-
Public Event StatusChanged As Action(Of String)
157-
158-
' Handlers with different signatures
159-
160-
' Exact match for EventHandler - no warning
161-
Private Sub OnDocumentProcessed_Exact(sender As Object, e As EventArgs)
162-
Console.WriteLine("Document processed (exact signature)")
163-
End Sub
164-
165-
' Simplified handler - causes BC42328 with EventHandler
166-
Private Sub OnDocumentProcessed_Simple()
167-
Console.WriteLine("Document processed (simple)")
168-
End Sub
169-
170-
' Handler for custom event - exact match
171-
Private Sub OnStatusChanged_Exact(message As String)
172-
Console.WriteLine($"Status: {message}")
173-
End Sub
174-
175-
' Handler with ignored parameters - causes BC42328 with custom event
176-
Private Sub OnStatusChanged_Simple()
177-
Console.WriteLine("Status changed")
178-
End Sub
179-
180-
Public Sub DemonstrateWarnings()
181-
Console.WriteLine("Setting up event handlers...")
182-
183-
' These work without warnings (exact matches)
184-
AddHandler DocumentProcessed, AddressOf OnDocumentProcessed_Exact
185-
AddHandler StatusChanged, AddressOf OnStatusChanged_Exact
186-
187-
' These generate BC42328 warnings (relaxed conversions)
188-
AddHandler DocumentProcessed, AddressOf OnDocumentProcessed_Simple
189-
AddHandler StatusChanged, AddressOf OnStatusChanged_Simple
190-
191-
' Fire the events
192-
RaiseEvent DocumentProcessed(Me, EventArgs.Empty)
193-
RaiseEvent StatusChanged("Processing complete")
194-
End Sub
195-
196-
Public Sub DemonstrateSolutions()
197-
Console.WriteLine("Using solutions to avoid warnings...")
198-
199-
' Solution 1: Assign to variable first
200-
Dim handler1 As EventHandler = AddressOf OnDocumentProcessed_Simple
201-
AddHandler DocumentProcessed, handler1
202-
203-
' Solution 2: Use lambda expression
204-
AddHandler DocumentProcessed, Sub(s, e) OnDocumentProcessed_Simple()
205-
206-
' Solution 3: Direct assignment to delegate variable
207-
Dim handler2 As Action(Of String) = AddressOf OnStatusChanged_Simple
208-
AddHandler StatusChanged, handler2
209-
210-
' Fire the events
211-
RaiseEvent DocumentProcessed(Me, EventArgs.Empty)
212-
RaiseEvent StatusChanged("All solutions work")
213-
End Sub
214-
End Class
215-
216-
Module Program
217-
Sub Main()
218-
Dim processor As New DocumentProcessor()
219-
220-
Console.WriteLine("=== Demonstrating BC42328 Warning ===")
221-
processor.DemonstrateWarnings()
222-
223-
Console.WriteLine()
224-
Console.WriteLine("=== Demonstrating Solutions ===")
225-
processor.DemonstrateSolutions()
226-
227-
Console.WriteLine("Press any key to exit...")
228-
Console.ReadKey()
229-
End Sub
230-
End Module
231-
```
148+
:::code language="vb" source="snippets/bc42328/AddressOfExample.vb":::
232149

233150
This example shows how the warning occurs in realistic scenarios and demonstrates multiple ways to resolve it.
234151

docs/visual-basic/misc/snippets/bc42328/AddressOfExample.vbproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<RootNamespace>BC42328Example</RootNamespace>
77
</PropertyGroup>
88

0 commit comments

Comments
 (0)