-
Notifications
You must be signed in to change notification settings - Fork 6k
Add comprehensive examples to BC42328.md to improve understanding #48310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+176
−41
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
36d4a12
Initial plan
Copilot 27c062e
Add comprehensive examples to BC42328.md to improve clarity
Copilot 41b8555
Apply suggestions from code review
BillWagner aee332a
Apply suggestions from code review
BillWagner 57bd2e4
Apply suggestions from code review
BillWagner bafe8e7
Move large inline code example to snippet file and use proper reference
Copilot f5891fb
Replace inline code with snippet reference to eliminate duplication
Copilot dae6006
Remove redundant "Complete working example" section as requested
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
docs/visual-basic/misc/snippets/bc42328/AddressOfExample.vb
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
Imports System | ||
|
||
' <DocumentProcessor> | ||
Public Class DocumentProcessor | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
' Standard .NET event using EventHandler | ||
Public Event DocumentProcessed As EventHandler | ||
|
||
' Custom event with different signature | ||
Public Event StatusChanged As Action(Of String) | ||
|
||
' Handlers with different signatures | ||
|
||
' Exact match for EventHandler - no warning | ||
Private Sub OnDocumentProcessed_Exact(sender As Object, e As EventArgs) | ||
Console.WriteLine("Document processed (exact signature)") | ||
End Sub | ||
|
||
' Simplified handler - causes BC42328 with EventHandler | ||
Private Sub OnDocumentProcessed_Simple() | ||
Console.WriteLine("Document processed (simple)") | ||
End Sub | ||
|
||
' Handler for custom event - exact match | ||
Private Sub OnStatusChanged_Exact(message As String) | ||
Console.WriteLine($"Status: {message}") | ||
End Sub | ||
|
||
' Handler with ignored parameters - causes BC42328 with custom event | ||
Private Sub OnStatusChanged_Simple() | ||
Console.WriteLine("Status changed") | ||
End Sub | ||
|
||
Public Sub DemonstrateWarnings() | ||
Console.WriteLine("Setting up event handlers...") | ||
|
||
' These work without warnings (exact matches) | ||
AddHandler DocumentProcessed, AddressOf OnDocumentProcessed_Exact | ||
AddHandler StatusChanged, AddressOf OnStatusChanged_Exact | ||
|
||
' These generate BC42328 warnings (relaxed conversions) | ||
AddHandler DocumentProcessed, AddressOf OnDocumentProcessed_Simple | ||
AddHandler StatusChanged, AddressOf OnStatusChanged_Simple | ||
|
||
' Fire the events | ||
RaiseEvent DocumentProcessed(Me, EventArgs.Empty) | ||
RaiseEvent StatusChanged("Processing complete") | ||
End Sub | ||
|
||
Public Sub DemonstrateSolutions() | ||
Console.WriteLine("Using solutions to avoid warnings...") | ||
|
||
' Solution 1: Assign to variable first | ||
Dim handler1 As EventHandler = AddressOf OnDocumentProcessed_Simple | ||
AddHandler DocumentProcessed, handler1 | ||
|
||
' Solution 2: Use lambda expression | ||
AddHandler DocumentProcessed, Sub(s, e) OnDocumentProcessed_Simple() | ||
|
||
' Solution 3: Direct assignment to delegate variable | ||
Dim handler2 As Action(Of String) = AddressOf OnStatusChanged_Simple | ||
AddHandler StatusChanged, handler2 | ||
|
||
' Fire the events | ||
RaiseEvent DocumentProcessed(Me, EventArgs.Empty) | ||
RaiseEvent StatusChanged("All solutions work") | ||
End Sub | ||
End Class | ||
' </DocumentProcessor> | ||
|
||
Module Program | ||
Sub Main() | ||
Dim processor As New DocumentProcessor() | ||
|
||
Console.WriteLine("=== Demonstrating BC42328 Warning ===") | ||
processor.DemonstrateWarnings() | ||
|
||
Console.WriteLine() | ||
Console.WriteLine("=== Demonstrating Solutions ===") | ||
processor.DemonstrateSolutions() | ||
|
||
Console.WriteLine("Press any key to exit...") | ||
Console.ReadKey() | ||
End Sub | ||
End Module |
9 changes: 9 additions & 0 deletions
9
docs/visual-basic/misc/snippets/bc42328/AddressOfExample.vbproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<RootNamespace>BC42328Example</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.