Solved

Casting : If cast fails else

Posted on 2006-06-19
2
192 Views
Last Modified: 2010-04-16
Is there a simple way to test a cast. For example I have a structure that changes from time to time, and if the cast fails move onto an else statement?

Is this possible or wi.l it always throw an exception? Basically what I get is saying that I cant convert my custom type to a bool. But there should be other ways.

Any ideas?
0
Comment
Question by:directxBOB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 16938828
You can test inheritance using the "is" keyword:

  if(myObject is MyStruct)
  {
    MyStruct myStruct = (MyStruct)myObject;
    //...
  }

But that will not definately check if a cast can be made.  If you were not dealing with a value type you could use the "as" keyword:

  MyClass myClass = myObject as MyClass;
  if (myClass != null)
  {
    //...
  }

Since you are dealing with structs, I think you will have to resort to:

  try
  {
    MyStruct myClass = (MyStruct)myObject;

    //...
  }
  catch(InvalidCastException)
  {
  }
0
 

Author Comment

by:directxBOB
ID: 16939009
Try catch was what I went for in the end. But thanks for the other alternatives very handy to know.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MailAddress in vb 4 36
LINQ Select all records by MAX data syntax help 1 30
How can I do Unit Testing with Session ? 1 23
parsing question on dynamic concatenation 3 34
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question