Click here to Skip to main content
11,412,516 members (70,497 online)
Click here to Skip to main content

Cheat Sheet - Casting in VB.NET and C#

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

Introduction

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)
    MessageBox.Show(t.FullName)
    
  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()
    MessageBox.Show(t.FullName)
    

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);
    MessageBox.Show(t.FullName);
    
  5. Obtaining a System.Type object for a given object can be done with the GetType method:

    System.Type t;
    t = obj.GetType();
    MessageBox.Show(t.FullName);
    

License

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

Share

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sibeesh Venu at 8-Aug-14 7:02
professionalSibeesh Venu8-Aug-14 7:02 
GeneralMy vote of 5 Pin
Raja Sekhar S at 3-Jun-13 20:05
professionalRaja Sekhar S3-Jun-13 20:05 
GeneralMy vote of 5 Pin
Heaven2020 at 18-Mar-13 4:11
memberHeaven202018-Mar-13 4:11 
GeneralMy vote of 4 Pin
batres-sc at 10-Mar-13 15:31
memberbatres-sc10-Mar-13 15:31 
GeneralMy vote of 5 Pin
JGeZau at 22-Oct-12 9:19
memberJGeZau22-Oct-12 9:19 
GeneralMy vote of 5 Pin
Mcwatt77 at 5-Oct-10 15:51
memberMcwatt775-Oct-10 15:51 
GeneralMy vote of 4 Pin
richardhenwood at 13-Jul-10 23:30
memberrichardhenwood13-Jul-10 23:30 
GeneralHi Pin
manchisrikanth at 10-Jun-10 3:22
membermanchisrikanth10-Jun-10 3:22 
GeneralA note about as Pin
Wjousts at 24-Jul-09 5:16
memberWjousts24-Jul-09 5:16 
GeneralConverting Variables Pin
SashoJ at 29-Jun-09 8:32
memberSashoJ29-Jun-09 8:32 
GeneralRe: Converting Variables Pin
Wjousts at 24-Jul-09 5:15
memberWjousts24-Jul-09 5:15 
GeneralMissing a lot Pin
JoshMouch at 25-Feb-07 15:27
memberJoshMouch25-Feb-07 15:27 
Generaldifference VB/C# in compile-time vs run-time error Pin
Alexis Rzewski at 7-Feb-07 5:51
memberAlexis Rzewski7-Feb-07 5:51 
QuestionHow about casting arrays Pin
eschneider100 at 19-Mar-05 9:57
membereschneider10019-Mar-05 9:57 
AnswerRe: How about casting arrays Pin
collinsauve at 7-Aug-08 4:28
membercollinsauve7-Aug-08 4:28 
GeneralToString Pin
George_Seremetidis at 28-Sep-04 14:40
memberGeorge_Seremetidis28-Sep-04 14:40 
GeneralDon't Forget System.Convert.ChangeType Pin
Shaun Hayward at 29-Jun-04 8:35
memberShaun Hayward29-Jun-04 8:35 
GeneralRe: Don't Forget System.Convert.ChangeType Pin
skjagini at 6-Sep-05 3:56
memberskjagini6-Sep-05 3:56 
JokeRe: Don't Forget System.Convert.ChangeType Pin
Artur M. at 7-Dec-06 22:45
memberArtur M.7-Dec-06 22:45 
GeneralProblems with casting objects Pin
simbakid at 3-Mar-04 4:04
membersimbakid3-Mar-04 4:04 
QuestionDifference between DirectCast and Ctype? Pin
FruitBatInShades at 26-Jan-04 6:36
memberFruitBatInShades26-Jan-04 6:36 
GeneralPlease add pointers to the next update! Pin
Anonymous at 4-Oct-03 14:20
sussAnonymous4-Oct-03 14:20 
GeneralOK, but could be better Pin
David Bennett at 30-Sep-03 5:23
sussDavid Bennett30-Sep-03 5:23 
GeneralInformative Pin
Nishant S at 23-Sep-03 21:14
editorNishant S23-Sep-03 21:14 
GeneralComments Pin
Nick Seng at 23-Sep-03 16:21
memberNick Seng23-Sep-03 16:21 
GeneralGood Pin
dog_spawn at 23-Sep-03 14:14
memberdog_spawn23-Sep-03 14:14 
GeneralRe: Good Pin
Furty at 23-Sep-03 14:36
memberFurty23-Sep-03 14:36 
GeneralRe: Good Pin
dog_spawn at 24-Sep-03 7:34
memberdog_spawn24-Sep-03 7:34 
GeneralRe: Good Pin
Rudi Breedenraedt at 25-Sep-03 6:22
memberRudi Breedenraedt25-Sep-03 6:22 
GeneralRe: Good Pin
dog_spawn at 25-Sep-03 7:52
memberdog_spawn25-Sep-03 7:52 
GeneralRe: Good Pin
peterchen at 29-Sep-03 7:29
memberpeterchen29-Sep-03 7:29 
GeneralRe: Good Pin
dog_spawn at 29-Sep-03 7:34
memberdog_spawn29-Sep-03 7:34 
GeneralRe: Good Pin
peterchen at 29-Sep-03 12:19
memberpeterchen29-Sep-03 12:19 
GeneralRe: Good Pin
dog_spawn at 29-Sep-03 12:23
memberdog_spawn29-Sep-03 12:23 
GeneralRe: Good Pin
peterchen at 29-Sep-03 13:26
memberpeterchen29-Sep-03 13:26 
GeneralRe: Good Pin
dog_spawn at 29-Sep-03 14:53
memberdog_spawn29-Sep-03 14:53 
GeneralRe: Good Pin
Rocky Moore at 23-Sep-03 15:14
memberRocky Moore23-Sep-03 15:14 
GeneralRe: Good Pin
Attila Hajdrik at 25-Sep-03 22:27
memberAttila Hajdrik25-Sep-03 22:27 
GeneralRe: Good Pin
rj45 at 24-Apr-04 22:07
memberrj4524-Apr-04 22:07 
GeneralRe: Good Pin
Ngraham at 26-May-04 5:32
memberNgraham26-May-04 5:32 
GeneralRe: Good Pin
DBuckner at 26-May-04 19:57
memberDBuckner26-May-04 19:57 

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

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

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