You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
' Using AddHandler with relaxed conversion - causes BC42328
47
-
AddHandlerSomeEvent,AddressOfHandler2
48
-
```
35
+
When you compile this code, Visual Basic shows the BC42328 warning for the `AddHandler` statements that use relaxed delegate conversion.
49
36
50
37
## Why AddressOf appears to have "no effect"
51
38
@@ -57,36 +44,72 @@ However, `AddressOf` is still syntactically required in `AddHandler` and `Remove
57
44
58
45
## To correct this error
59
46
60
-
You have several options depending on your needs:
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:
61
48
62
-
-**Assign to a variable first (preserves exact semantics):**
49
+
### Option 1: Assign to a variable first (preserves exact semantics)
63
50
64
-
```vb
65
-
DimhandlerAsEventHandler=AddressOfHandler2
66
-
AddHandlerSomeEvent,handler
67
-
```
51
+
```vb
52
+
PublicSubDemonstrateHandler()
53
+
' Create delegate variable first - this eliminates the warning
@@ -95,8 +118,27 @@ This warning is often safe to ignore when:
95
118
- Your handler method intentionally omits parameters it doesn't need.
96
119
- You're using a simpler method signature for cleaner code.
97
120
- The performance impact is negligible for your application.
121
+
- You're prototyping or writing quick utility code where clarity matters more than efficiency.
122
+
123
+
The relaxed conversion works correctly; this warning just indicates a minor efficiency consideration. The compiler creates a wrapper delegate automatically, which adds a small performance overhead but doesn't affect functionality.
98
124
99
-
The relaxed conversion works correctly; this warning just indicates a minor efficiency consideration.
125
+
### Example of when ignoring is appropriate
126
+
127
+
```vb
128
+
PublicClassQuickLogger
129
+
PublicEventLogMessageAsAction(OfString,DateTime)
130
+
131
+
' Simple handler that ignores the timestamp parameter
132
+
PrivateSubWriteToConsole()
133
+
Console.WriteLine("Something was logged")
134
+
EndSub
135
+
136
+
PublicSubSetupLogging()
137
+
' This generates BC42328, but it's fine for simple scenarios
0 commit comments