Skip to content

Commit f5891fb

Browse files
CopilotBillWagner
andcommitted
Replace inline code with snippet reference to eliminate duplication
Co-authored-by: BillWagner <[email protected]>
1 parent bafe8e7 commit f5891fb

File tree

2 files changed

+23
-51
lines changed

2 files changed

+23
-51
lines changed

docs/visual-basic/misc/bc42328.md

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,9 @@ This warning appears when Visual Basic can handle the assignment through relaxed
3030

3131
The following example shows one scenario that produces BC42328:
3232

33-
```vb
34-
Public Class FileProcessor
35-
' Define an event using the standard EventHandler delegate
36-
' EventHandler expects: Sub(sender As Object, e As EventArgs)
37-
Public Event ProcessingComplete As EventHandler
38-
39-
' Method with exact signature - no warning
40-
Private Sub OnProcessingComplete(sender As Object, e As EventArgs)
41-
Console.WriteLine($"Processing completed by {sender}")
42-
End Sub
43-
44-
' Method with simplified signature - this causes BC42328 warning
45-
Private Sub OnSimpleComplete()
46-
Console.WriteLine("Processing is done!")
47-
End Sub
48-
49-
Public Sub StartProcessing()
50-
' This works fine - exact signature match
51-
AddHandler ProcessingComplete, AddressOf OnProcessingComplete
52-
53-
' This generates BC42328 warning:
54-
' "The 'AddressOf' expression has no effect in this context because
55-
' the method argument to 'AddressOf' requires a relaxed conversion
56-
' to the delegate type of the event"
57-
AddHandler ProcessingComplete, AddressOf OnSimpleComplete
58-
59-
' Simulate completion
60-
RaiseEvent ProcessingComplete(Me, EventArgs.Empty)
61-
End Sub
62-
End Class
63-
```
33+
:::code language="vb" source="snippets/bc42328/AddressOfExample.vb" id="DocumentProcessor":::
6434

65-
When you compile this code, Visual Basic shows the BC42328 warning for the second `AddHandler` statement because `OnSimpleComplete` doesn't match the `EventHandler` signature exactly.
35+
When you compile this code, Visual Basic shows the BC42328 warning for the `AddHandler` statements that use relaxed delegate conversion.
6636

6737
## Why AddressOf appears to have "no effect"
6838

@@ -74,70 +44,70 @@ However, `AddressOf` is still syntactically required in `AddHandler` and `Remove
7444

7545
## To correct this error
7646

77-
You have several options depending on your needs. Using the `FileProcessor` example from previously, here are the different ways to resolve the BC42328 warning:
47+
You have several options depending on your needs. Using the `DocumentProcessor` example from previously, here are the different ways to resolve the BC42328 warning:
7848

7949
### Option 1: Assign to a variable first (preserves exact semantics)
8050

8151
```vb
82-
Public Sub StartProcessing()
52+
Public Sub DemonstrateHandler()
8353
' Create delegate variable first - this eliminates the warning
84-
Dim handler As EventHandler = AddressOf OnSimpleComplete
85-
AddHandler ProcessingComplete, handler
54+
Dim handler As EventHandler = AddressOf OnDocumentProcessed_Simple
55+
AddHandler DocumentProcessed, handler
8656
End Sub
8757
```
8858

8959
### Option 2: Match the delegate signature exactly
9060

9161
```vb
9262
' Change the method signature to match EventHandler exactly
93-
Private Sub OnSimpleComplete(sender As Object, e As EventArgs)
63+
Private Sub OnDocumentProcessed_Simple(sender As Object, e As EventArgs)
9464
' You can ignore the parameters if you don't need them
95-
Console.WriteLine("Processing is done!")
65+
Console.WriteLine("Document processed (simple)")
9666
End Sub
9767

98-
Public Sub StartProcessing()
68+
Public Sub DemonstrateHandler()
9969
' Now this works without warning
100-
AddHandler ProcessingComplete, AddressOf OnSimpleComplete
70+
AddHandler DocumentProcessed, AddressOf OnDocumentProcessed_Simple
10171
End Sub
10272
```
10373

10474
### Option 3: Use a lambda expression
10575

10676
```vb
107-
Public Sub StartProcessing()
77+
Public Sub DemonstrateHandler()
10878
' Wrap the method call in a lambda that matches the signature
109-
AddHandler ProcessingComplete, Sub(sender, e) OnSimpleComplete()
79+
AddHandler DocumentProcessed, Sub(sender, e) OnDocumentProcessed_Simple()
11080

11181
' Or create a more complex lambda inline
112-
AddHandler ProcessingComplete,
82+
AddHandler DocumentProcessed,
11383
Sub(sender, e)
11484
Console.WriteLine($"Event from {sender}")
115-
OnSimpleComplete()
85+
OnDocumentProcessed_Simple()
11686
End Sub
11787
End Sub
11888
```
11989

12090
### Option 4: Use the `Handles` clause (if appropriate)
12191

12292
```vb
123-
Public Class FileProcessor
93+
Public Class DocumentHandler
12494
' Declare the event source with WithEvents
125-
Private WithEvents processor As FileProcessor
95+
Private WithEvents processor As DocumentProcessor
12696

12797
' Use Handles clause - no AddHandler needed
128-
Private Sub OnSimpleComplete() Handles processor.ProcessingComplete
129-
Console.WriteLine("Processing is done!")
98+
Private Sub OnDocumentComplete() Handles processor.DocumentProcessed
99+
Console.WriteLine("Document processing is done!")
130100
End Sub
131101
End Class
132102
```
133103

134104
### Option 5: Direct assignment to delegate variable
135105

136106
```vb
137-
Public Sub StartProcessing()
107+
Public Sub DemonstrateHandler()
138108
' Assign to delegate variable then use with AddHandler
139-
Dim handler As EventHandler = AddressOf OnSimpleComplete
140-
AddHandler ProcessingComplete, handler
109+
Dim handler As EventHandler = AddressOf OnDocumentProcessed_Simple
110+
AddHandler DocumentProcessed, handler
141111
End Sub
142112
```
143113

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Imports System
22

3+
' <DocumentProcessor>
34
Public Class DocumentProcessor
45
' Standard .NET event using EventHandler
56
Public Event DocumentProcessed As EventHandler
@@ -64,6 +65,7 @@ Public Class DocumentProcessor
6465
RaiseEvent StatusChanged("All solutions work")
6566
End Sub
6667
End Class
68+
' </DocumentProcessor>
6769

6870
Module Program
6971
Sub Main()

0 commit comments

Comments
 (0)