?
Solved

Casting : If cast fails else

Posted on 2006-06-19
2
Medium Priority
?
195 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

770 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