Difference between directcast and ctype

Showing Answers 1 - 1 of 1 Answers

saroj

  • Nov 22nd, 2006
 

If you're totally sure that the expression is the same type that it's being cast to, use DirectCast. If there is any doubt, use CType.

 

The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type and DirectCast requires that the run-time type of an object variable to be the same as the specified type that it's being cast to.  Really the same, not just that one can be converted to the other.

Use DirectCast if you're absolutely positively sure that an object is the specified type and the run-time type of the expression are the same.  If you're not sure but expect that the conversion will work, use CType.   The run-time performance of DirectCast is better than that of CType. However, DirectCast throws an InvalidCastException error if the argument types do not match, so you must be sure.

 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions