VB.NET Working with Delegate and Events

Introduction

From hobbyist to seasoned developers delegates are avoided or attempts are made to understand them, which in many cases is met with frustration. I see questions on various forums where code could be refactored by using delegates and when making suggestions to use delegates generally the response is something like “that is over my head, what I have works!”. Well just because code works does not mean there is room for refactoring.

Search the Internet for VB.NET delegates and find out there are an abundance of post while the majority are not fully working examples and may be untested which can lead back to frustration.

The first mistake some coders make is not reading Microsoft documentation on delegates or think outside the box and study C# code samples and a decent C# to Visual Basic converter as there are more code samples in C# than Visual Basic. One must understand that there are cases like with MulticastDelegate class there is syntactic difference on how to use them when seeking code samples in C# so best to go to the Microsoft documentation but don't expect to find many code samples in Visual Basic 

How to properly learn

First devote time to reading documentation rather than viewing code samples found on the Internet that are not real world code samples but instead bits and pieces which can (repeating myself but well worth it) lead back to frustration when a coder gets stuck because code samples found are not full code samples.

Once documentation has been read, read it again then look at my code samples, run each code sample without studying the code, then go back and inspect the code followed by using Visual Studio debugger to trace through the code.

What not to do

  • Not read documentation
  • Copy and paste code into a project and expect it to work.
  • Do not try and fit code samples into a project just to try them out without a reason such as with the builder pattern code sample.
Code samples

Currently there are 20 code samples in a GitHub repository where none of the code samples interact with a database on purpose so you don't need to be concerned with setting up database and deal with all that goes with this especially if I were to use Oracle for a database rather than SQL-Server or Microsoft Access.

Anytime a code sample needs data the data is coming from text files (could had also used XML and/or JSON) where the text files are comma delimited with no issues e.g. wrong data types for instance.

Some of the code samples for some may be in new territories e.g. extracting files from a compressed file or dealing with how to work with temp files to ensure they get deleted when the application closes.

A good deal of the code samples are asynchronous oriented so for some there are bonuses.

There is only one WPF code sample (note that in this code samples no controls have names set) as the majority of coders in forums are not using WPF (I'm sure there are plenty out there but not the same as C#). If readers would like to see more WPF code samples let me know. 

Building the Visual Studio 2017 solution

Once the solution has been loaded into Visual Studio, from solution explorer right click on the top tree node and select "Restore NuGet Packages" followed by building the solution.

Code will work with Visual Studio 2019 also, no test done with lower editions of Visual Studio.

Is there a copyright to use the code
One of my things over the past 20 plus years has been to share, no restrictions, no copyrights needed to be placed into your code but if you use code without alterations (not very likely) and indicate your wrote the code include my name as the original author. 

In closing

Over time more code samples will be added the repository or another repository will be created that go into working with databases, Entity Framework Core and WPF.

Much of the code is documented but not to extremes, that is what Microsoft documentation is for along with taking appropriate time to learn in test projects before using delegates in a active project.

Comments

  1. Ok! Straight forward enough. Let's take a look at the repo and see what light your samples throw on delegates.

    ReplyDelete
  2. thx for the good work, Karen. very much appreciated!
    yes, more WPF samples please :-)

    ReplyDelete

Post a Comment

Popular posts from this blog

Coders asking questions in online forums

Sometimes VB.NET coders make me wonder (or crazy)