Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Getting the one and only Type of an Object

4.85/5 (6 votes)
7 Oct 2011CPOL1 min read 25.3K  
How to check an Objects Type, ignoring all subtypes, in C# and VB.
Last week, I had a problem with types and derived types. I wanted to execute some code when a type of SomeButton was clicked. Unfortunately, the piece of code was pretty generic and basically any type of Object could pass my piece of code. The result was that my code did not only execute when SomeButton was clicked, but also when any derived Class of SomeButton was clicked. That was when I learned the subtle difference between instance is Type (C#), TypeOf instance Is Type (VB) and GetType. Below is the code for both C# and VB to check the exact Type of an Object.
This is a Console Application, so you can simply copy/paste the complete example into a new Console Application Project and see the differences for yourself.

C#:
C#
using System;

namespace TypeTipCSharp
{
	class Base { }
	class Derived : Base { }

	class Program
	{
		static void Main(string[] args)
		{
			Derived d = new Derived();
			Console.WriteLine("Variable d is an instance of Derived, which inherits from Base.");

			if (d is Base)
			{ Console.WriteLine("The type of d is Base"); }
			else
			{ Console.WriteLine("The type of d is not Base"); }

			if (d.GetType() == typeof(Base))
			{ Console.WriteLine("The GetType of d is Base"); }
			else
			{ Console.WriteLine("The GetType of d is not Base"); }

			Base b = new Derived();
			Console.WriteLine("{0}Variable b is of Type Base, but has an instance of Derived assigned to it.", Environment.NewLine);

			if (b is Base)
			{ Console.WriteLine("The type of b is Base"); }
			else
			{ Console.WriteLine("The type of b is not Base"); }

			if (b.GetType() == typeof(Base))
			{ Console.WriteLine("The GetType of b is Base"); }
			else
			{ Console.WriteLine("The GetType of b is not Base"); }

			if (b is Derived)
			{ Console.WriteLine("The type of b is Derived"); }
			else
			{ Console.WriteLine("The type of b is not Derived"); }

			if (b.GetType() == typeof(Derived))
			{ Console.WriteLine("The GetType of b is Derived"); }
			else
			{ Console.WriteLine("The GetType of b is not Derived"); }

			Console.WriteLine("{0}Press a key to close.", Environment.NewLine);
			Console.ReadKey();

		}
	}
}


VB:
VB
Public Class Base
End Class

Public Class Derived
	Inherits Base
End Class

Module Module1

	Sub Main()
		Dim d As New Derived
		Console.WriteLine("Variable d is an instance of Derived, which inherits from Base.")

		If TypeOf d Is Base Then
			Console.WriteLine("The type of d is Base")
		Else
			Console.WriteLine("The type of d is not Base")
		End If

		If d.GetType = GetType(Base) Then
			Console.WriteLine("The GetType of d is Base")
		Else
			Console.WriteLine("The GetType of d is not Base")
		End If

		Dim b As Base = New Derived
		Console.WriteLine("{0}Variable b is of Type Base, but has an instance of Derived assigned to it.", Environment.NewLine)

		If TypeOf b Is Base Then
			Console.WriteLine("The type of b is Base")
		Else
			Console.WriteLine("The type of b is not Base")
		End If

		If b.GetType = GetType(Base) Then
			Console.WriteLine("The GetType of b is Base")
		Else
			Console.WriteLine("The GetType of b is not Base")
		End If

		If TypeOf b Is Derived Then
			Console.WriteLine("The type of b is Derived")
		Else
			Console.WriteLine("The type of b is not Derived")
		End If

		If b.GetType = GetType(Derived) Then
			Console.WriteLine("The GetType of b is Derived")
		Else
			Console.WriteLine("The GetType of b is not Derived")
		End If

		Console.WriteLine("{0}Press a key to close.", Environment.NewLine)
		Console.ReadKey()
	End Sub

End Module


The result:
Variable d is an instance of Derived, which inherits from Base.
The type of d is Base
The GetType of d is not Base

Variable b is of Type Base, but has an instance of Derived assigned to it.
The type of b is Base
The GetType of b is not Base
The type of b is Derived
The GetType of b is Derived


So GetType will always return the exact type of an Object which can be compared to a Type. Only when the two types have an exact match do they really match. As you can see, Derived is a type of Base, but Derived.GetType does not match Base.
instance is Type and TypeOf instance Is Type return True whenever the instance is of the type Type or any derived type!
The difference is subtle, but very important. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)