@@ -145,90 +145,7 @@ End Sub
145145
146146The 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
233150This example shows how the warning occurs in realistic scenarios and demonstrates multiple ways to resolve it.
234151
0 commit comments