Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 761267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:49:03+00:00 2026-05-14T15:49:03+00:00

Ever since I moved from VB6 to VB.NET somewhere in 2005, I’ve been using

  • 0

Ever since I moved from VB6 to VB.NET somewhere in 2005, I’ve been using CType to do casting from one data type to another. I do this because it is simply faster to type, used to exist in VB6 and I do not know why I have to be using DirectCast if there is apparently no difference between them.

I use TryCast once in a while because I understand that sometimes casting can fail. I however cannot get the difference between CType and DirectCast.

Can anyone tell me the difference in plain simple English what the difference the two (CType and DirectCast)? Adding examples of where to use what as well would be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:49:04+00:00Added an answer on May 14, 2026 at 3:49 pm

    TryCast and DirectCast are casting operators that directly map to the CLR’s support for casting. They can quickly cast an object of a base type to a derived type or unbox a value of a value type. DirectCast throws an exception when the cast isn’t possible, TryCast returns Nothing if it failed. You typically want to favor DirectCast to catch programming mistakes.

    CType allows a superset of conversions, ones that the CLR frowns on. The best example I can think of is converting a string to a number or date. For example:

    Dim obj As Object
    obj = "4/1/2010"
    Dim dt As DateTime = CType(obj, DateTime)
    

    Which you’ll have to use if Option Strict On is in effect. If it is Off then you can do it directly:

    Option Strict Off
    ...
        Dim dt As DateTime = obj
    

    Very convenient of course and part of VB.NET’s legacy as a dynamically typed language. But not without problems, that date is Unicorn day at stackoverflow.com but will be a day in January when a Briton enters the string. Unexpected conversions is the reason the CLR doesn’t permit these directly. The explicit, never a surprise conversion looks like this:

    Dim dt As DateTime = DateTime.Parse(obj.ToString(), _
        System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat)
    

    Whether you should buy into Try/DirectCast vs CType vs explicit conversions is rather a personal choice. If you now program with Option Strict On then you should definitely start using Try/DirectCast. If you favor the VB.NET language because you like the convenience of dynamic typing then don’t hesitate to stay on CType.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've moved a WebBBS board from one server to another. Ever since the board
Ever since I started using .NET, I've just been creating Helper classes or Partial
I guess I have been using the Web Site model ever since .NET 2.0
Ever since switching from TABLE-layout to DIV-layout, one common problem remains: PROBLEM : you
Ever since the publication of Enterprise Integration Patterns people have been using the notation
that one has been nagging me ever since I dabbled in web developpement; is
I'd appreciate people's thoughts on a dilemma I've been struggling with ever since ASP.NET
Ever since JQuery came along a few years ago I've been using it in
Ever since Microsoft has introduced the application blocks, I've been bumping into people who
I'm getting the following error, ever since I moved my DataTable to a different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.