Skip to content

Commit 7597957

Browse files
CopilotBillWagnergewarren
authored
Improve BC42328 documentation with comprehensive explanation of relaxed delegate conversions (#48120)
* Initial plan * Enhanced BC42328 documentation with comprehensive explanation of relaxed delegate conversions Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> Co-authored-by: Bill Wagner <wiwagn@microsoft.com> Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
1 parent a4e56c9 commit 7597957

File tree

1 file changed

+91
-5
lines changed

1 file changed

+91
-5
lines changed

docs/visual-basic/misc/bc42328.md

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,98 @@ helpviewer_keywords:
1010
ms.assetid: 1561e5bf-b8ab-4a67-990d-c3a76e2ab353
1111
---
1212
# The 'AddressOf' expression has no effect in this context because the method argument to 'AddressOf' requires a relaxed conversion to the delegate type of the event
13-
14-
## To correct this error
15-
16-
1. Assign the 'AddressOf' expression to a variable, and use the variable to add or remove the method as the handler.
17-
13+
14+
This warning occurs when you use `AddressOf` with a method in an event handler context where the method signature doesn't exactly match the event's delegate signature. Visual Basic allows *relaxed delegate conversion* in these scenarios, but the `AddressOf` operator becomes redundant because the compiler must create a wrapper delegate anyway.
15+
16+
## Understand relaxed delegate conversion
17+
18+
Visual Basic allows you to assign methods to delegates even when their signatures don't match exactly, as long as the conversion is safe. This is called *relaxed delegate conversion* and includes:
19+
20+
- **Widened parameter types**: A method parameter can be a wider type than the delegate parameter.
21+
- **Widened return types**: A method return type can be narrower than the delegate return type.
22+
- **Omitted parameters**: A method can have fewer parameters than the delegate.
23+
- **Dropped return values**: A function can be assigned to a `Sub` delegate.
24+
25+
## When this warning occurs
26+
27+
This warning appears when Visual Basic can handle the assignment through relaxed conversion, making the explicit `AddressOf` unnecessary. For example:
28+
29+
```vb
30+
' Event delegate expects (sender As Object, e As EventArgs)
31+
Public Event SomeEvent As EventHandler
32+
33+
' Method with exact signature - no warning
34+
Sub Handler1(sender As Object, e As EventArgs)
35+
' Implementation
36+
End Sub
37+
38+
' Method with omitted parameters - causes BC42328
39+
Sub Handler2()
40+
' Implementation
41+
End Sub
42+
43+
' Using AddHandler with exact match - no warning
44+
AddHandler SomeEvent, AddressOf Handler1
45+
46+
' Using AddHandler with relaxed conversion - causes BC42328
47+
AddHandler SomeEvent, AddressOf Handler2
48+
```
49+
50+
## Why AddressOf appears to have "no effect"
51+
52+
The warning message states that `AddressOf` "has no effect" because Visual Basic must create a wrapper delegate for the relaxed conversion regardless. The `AddressOf` operator doesn't change how the compiler handles the conversion: the same result occurs whether you use `AddressOf` or not in relaxed conversion scenarios.
53+
54+
However, `AddressOf` is still syntactically required in `AddHandler` and `RemoveHandler` statements.
55+
56+
**Error ID:** BC42328
57+
58+
## To correct this error
59+
60+
You have several options depending on your needs:
61+
62+
- **Assign to a variable first (preserves exact semantics):**
63+
64+
```vb
65+
Dim handler As EventHandler = AddressOf Handler2
66+
AddHandler SomeEvent, handler
67+
```
68+
69+
- **Match the delegate signature exactly:**
70+
71+
```vb
72+
Sub Handler2(sender As Object, e As EventArgs)
73+
' Implementation - use parameters or ignore them
74+
End Sub
75+
```
76+
77+
- **Use a lambda expression:**
78+
79+
```vb
80+
AddHandler SomeEvent, Sub() Handler2()
81+
```
82+
83+
- **Use the `Handles` clause if appropriate:**
84+
85+
```vb
86+
Sub Handler2() Handles someObject.SomeEvent
87+
' Implementation
88+
End Sub
89+
```
90+
91+
## When you can ignore this warning
92+
93+
This warning is often safe to ignore when:
94+
95+
- Your handler method intentionally omits parameters it doesn't need.
96+
- You're using a simpler method signature for cleaner code.
97+
- The performance impact is negligible for your application.
98+
99+
The relaxed conversion works correctly; this warning just indicates a minor efficiency consideration.
100+
18101
## See also
19102

20103
- [AddressOf Operator](../language-reference/operators/addressof-operator.md)
21104
- [Events (Visual Basic)](../programming-guide/language-features/events/index.md)
105+
- [Relaxed Delegate Conversion](../programming-guide/language-features/delegates/relaxed-delegate-conversion.md)
106+
- [AddHandler Statement](../language-reference/statements/addhandler-statement.md)
107+
- [RemoveHandler Statement](../language-reference/statements/removehandler-statement.md)

0 commit comments

Comments
 (0)