Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Casting : If cast fails else

Posted on 2006-06-19
2
Medium Priority
?
196 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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