Click here to Skip to main content
13,404,312 members (45,255 online)
Click here to Skip to main content
Add your own
alternative version


93 bookmarked
Posted 22 Sep 2003

Cheat Sheet - Casting in VB.NET and C#

, 22 Sep 2003
Rate this:
Please Sign up or sign in to vote.
Describes several casting and type related operations in VB.NET and C#.


This article describes several casting and type related operations in VB.NET and C#.

Casting in VB.NET

  1. By default in VB, casting is automatically done for you when you assign objects to variables. The objects are then automatically casted to the variables' type.

    This behaviour can be influenced by an option line on top of your code file:

    Option Strict On
    Option Strict Off

    When on, casting is strict and not automatic.

  2. Explicit casting can be done with the cast operator CType() or DirectCast():

    textbox = CType(obj, TextBox)
    textbox = DirectCast(obj, TextBox)

    The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType. DirectCast throws an InvalidCastException error if the argument types do not match.

  3. Testing if an object is of a particular type, can be done with the TypeOf...Is operator:

    If TypeOf obj Is TextBox Then...
  4. Obtaining a System.Type object for a given type can be done with the GetType operator:

    Dim t As System.Type
    t = GetType(String)
  5. Obtaining a System.Type object for a given object can be done with the GetType method:

    Dim t as System.Type
    t = obj.GetType()

Casting in C#

  1. C# is a strictly typed language. Whenever types don't match, casting is necessary.

    Regular casting in C# follows the C(++) and Java syntax:

    string s = (string)obj;

    The casting operator applies to the complete chain on the right of it, so in the following example, not a, but a.b is casted to a Form:

    Form f = (Form)a.b;

    To cast parts of the chain, use brackets. In the following example, obj is casted to a Form:

    string s = ((Form)obj).Text;
  2. C# knows an additional casting operator: as.

    The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. In the following situation, btn gets the value null:

    Object obj = new TextBox();
    Button btn = obj as Button;
  3. Testing if an object is of a particular type, can be done with the is operator:

    if (obj is TextBox) {...}
  4. Obtaining a System.Type object for a given type can be done with the typeof operator:

    System.Type t;
    t = typeof(String);
  5. Obtaining a System.Type object for a given object can be done with the GetType method:

    System.Type t;
    t = obj.GetType();


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Rudi Breedenraedt
Architect RealDolmen
Belgium Belgium
Rudi is a Software Architect at RealDolmen.

You may also be interested in...

Comments and Discussions

GeneralRe: Good Pin
dog_spawn24-Sep-03 7:34
memberdog_spawn24-Sep-03 7:34 
GeneralRe: Good Pin
Rudi Breedenraedt25-Sep-03 6:22
memberRudi Breedenraedt25-Sep-03 6:22 
GeneralRe: Good Pin
dog_spawn25-Sep-03 7:52
memberdog_spawn25-Sep-03 7:52 
GeneralRe: Good Pin
peterchen29-Sep-03 7:29
memberpeterchen29-Sep-03 7:29 
GeneralRe: Good Pin
dog_spawn29-Sep-03 7:34
memberdog_spawn29-Sep-03 7:34 
GeneralRe: Good Pin
peterchen29-Sep-03 12:19
memberpeterchen29-Sep-03 12:19 
GeneralRe: Good Pin
dog_spawn29-Sep-03 12:23
memberdog_spawn29-Sep-03 12:23 
GeneralRe: Good Pin
peterchen29-Sep-03 13:26
memberpeterchen29-Sep-03 13:26 
dog_spawn wrote:
Please have mercy

No Cool | :cool:

If you don't want to discuss it, and you can keep yourself from replying, don't reply.


dog_spawn wrote:
I assume you already know that

lets, for this discussion, assume I don't.

dog_spawn wrote:
in good design you do not need to downcast because the type of the object you are dealing with should already be the correct interface

This propagates requirement of knowledge of this interface through large parts of the project, In my experience, tight coupling is always the downfall of large projects, so I tend to reduce the coupling to the absolute minimum.

Do you accept the "virtue" of QueryInterface - or is this "wrong" too?

dog_spawn wrote:
That is basic OO programming using types

OO is no religion, just a set of rules that have reasons. In programming, I refuse to accept a rule when noone can tell me the reason.

"Vierteile den, der sie Hure schimpft mit einem türkischen Säbel."

sighist | Agile Programming | doxygen

GeneralRe: Good Pin
dog_spawn29-Sep-03 14:53
memberdog_spawn29-Sep-03 14:53 
GeneralRe: Good Pin
Rocky Moore23-Sep-03 15:14
memberRocky Moore23-Sep-03 15:14 
GeneralRe: Good Pin
Attila Hajdrik25-Sep-03 22:27
memberAttila Hajdrik25-Sep-03 22:27 
GeneralRe: Good Pin
rj4524-Apr-04 22:07
memberrj4524-Apr-04 22:07 
GeneralRe: Good Pin
Ngraham26-May-04 5:32
memberNgraham26-May-04 5:32 
GeneralRe: Good Pin
DBuckner26-May-04 19:57
memberDBuckner26-May-04 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1802120.2 | Last Updated 23 Sep 2003
Article Copyright 2003 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid