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

  • Home
  • SEARCH
  • 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 1031313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:54:29+00:00 2026-05-16T13:54:29+00:00

I find this behavior of TryCast in .NET 4.0 / VS 2010 rather confusing.

  • 0

I find this behavior of TryCast in .NET 4.0 / VS 2010 rather confusing.

In my understanding TryCast works like DirectCast, but will return Nothing instead of throwing an exception if a cast is not possible.

VS 2010 / .NET 4

?TryCast(CType(1, Object), String)
Nothing
?DirectCast(CType(1, Object), String)
"1"

VS 2008 / .NET 3.5

?TryCast(CType(1, Object), String)
Nothing
?DirectCast(CType(1, Object), String)
Cannot convert to 'String'.

The .NET 3.5 results are consistent with what I believe TryCast does… .NET 4 however is not.

Can someone please point me in the best direction to securely cast an object to String in .NET 4?

  • 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-16T13:54:29+00:00Added an answer on May 16, 2026 at 1:54 pm

    Based on your code samples starting with the ? I’m guessing you’re using the immediate window to perform your test correct? The problem with this approach is that the immediate window is an interpretation instead of an actual evaluation. This leaves it susceptible to subtle corner case bugs and this is indeed one of them.

    If you take your sample code and add it to a simple VB.Net console application you’ll find that the 2010 behavior is identical to the 2008 behavior (throws an exception).

    EDIT

    So why did this regression happen? In 2010 I completely rewrote the VB EE debugging engine (expression evaluator). The older code base I inherited was simply too costly to maintain anymore. To the point that adding new features to the engine was more expensive that rewriting it from scratch with a better architecture that included the new features.

    As said before debugging evaluations is an interpretation more than an execution of code. It forces the duplication of some algorithms between the EE and CLR / Compiler. One of the areas where duplication occurs is in the casting logic. There is no way to ask the CLR debugger to cast a debug time object, it’s the responsibility of the EE to determine if the language specified cast is indeed valid.

    The old EE casting logic had numerous bugs (especially in the area of generics and arrays). The newer infrastructure conforms very closely to the CLR guidelines. However you’ll never have 100% parity because it would disallow very useful expressions in the EE (I may write a blog post on this in the future). But for most cases the behavior holds.

    In this particular instance I added a subtle bug which allows for a DirectCast of a value which is typed to Object to use VB’s Runtime Conversion operators vs. the specified behavior which only allows for CLR conversions. Hence this conversion succeeds where it should fail.

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

Sidebar

Related Questions

I have searched but could not find the reason for this behavior. I have
I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is
I can test this to find the behavior but that's not the point. In
I find this very strange, must be something I'm doing wrong, but still... I'm
I find this pretty confusing. When you want to rotate a view, it's going
i find this question tricky but what files store method signature info for xml
I could probably find this out from somewhere else but thought it would be
I find this behavior of the global window object a little weird. var x
I have code like below. If I open the file $File::Find::name (in this case
I'm struggling to find a Perl GetOptions reference that explains this behavior. If I

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.