VB.Net: Determine if an Object or Class Implements an Interface

The Problem

You want to know if a class called MyClass implements interface IMyInterface, or the object myObject inplements interface IMyInterface.
Unfortunately, you can’t do:

The Solution

It isn’t exactly intuitive, but for a class you can do this:

Or for an object:

Subtle Bug: VB.Net Dim Statement

Here is a question for you – are the following pieces of code equivalent?

Declaration followed by Explicit Initializer

Declaration with Implicit Implicit Initializer

If you try them for yourself, you’ll see that they both produce the same result in the console:
False
Why? According to MSDN:

If you do not specify initializer for a variable, Visual Basic initializes it to the default value for its data type. MSDN

So, VB sets the value of b to False, the default for a boolean type.
But you knew that already, didn’t you?
However, assuming that two pieces of code that produce the same result are exactly equivalent is a mistake.
So, try these two:

Declaration followed by Explicit Initializer, in a Loop

Declaration with Implicit Implicit Initializer in a Loop

This time, you’ll find that the two pieces of code produce different results. The first one will print:

False

False

False

False

False

While the second one will print:

False

True

True

True

True

The reason is that, in VB.Net, a Dim is a declaration, not a statement. As such, it is only used to set up the variable on the first pass, and in consequence the default initializer is only invoked on the first pass.

GridView Buttons not Working?

In my experience, the most common reason for Buttons in a GridView not working is that data is being bound to the GridView during the postback.

In other words, the code looks something like this:

When it should look like this: