Shout !


Free shoutbox @ ShoutMix

BlogMalaysia.com

CType | DirectCast | TryCast

Okies , i search for some code reference today, and accidentally found issues about type conversion in .net

There 3 types of cast , and based from what i read , i'll try to share it here.

CType - I think this is the commonly used in converting type. In our project, CType is mostly used in casting object from FindControl to the original type or from session to object such as dataTable. It need 2 parameter , the object and the new type. This function is included in .net by using Visual Basic namespace. Just as i mention before , this is maybe the most commonly used for type conversion because it is not strict. You can cast an object from string to integer, as long it have a valid value , such as "1234"

DirectCast - Using DirectCast , you can only convert a type to another type that has inheritance or implementation relationship. I can't really explain this , but from what i read, using directCast for a double to integer ,will throw an exception because there are no relationship. But we can cast System.Windows.Forms.Form to System.Windows.Forms.Control. We can see that , both of this type inherit from the FORMS. Benefit of using this compare to CType is better perfomance because it doesn't rely on Visual Basic routine.

TryCast - This one is quite interesting to me, because CType , DirectCast will throw an exception when it fail to convert type. But, by using TryCast , we will get NOTHING. So , why do you want to write codes for handle exception when TryCast can handle it for you? Ok, we got NOTHING, proceed to the next code. Yeah, thats cool. But theres a limitation here, you can only convert reference types, such as classes and interfaces and it have the same condition as DirectCast , from and to types that have relationship.

So, which one to use? Depends , but i think most of the time , it would be better if we use DirectCast , speed wise and we'll be more careful when declaring objects (If you know your variables have a value of "1234" , why declare it as a string , use integer instead (from here we should go deep , is it Int16,Int32 or Int64?))

Ok then , if you mind , please leave some comment , and if there any mistake in my post (and my english), please correct me

Thanks for reading this

6 ulasan:

peah @ peps berkata...

...apit...I've used CTYPE before for data type casting..the others are extra knowledge for me...my comment for you..the actual phrase is " if you don't mind "....:P

nagakeciks berkata...

Thanks Peps for your comment. Ok , i'll change it to "don't" :-)

peah @ peps berkata...

u're most welcome...keep up flooding us with your knowledge ya...

ashiena berkata...

thank you for enlighten me..

Radius berkata...

First comment:
Good that you already start blogging.

Second comment:
Very good that that you are blogging on something that useful unlike me merepek meraban on the net

Third comment:
I cant really remember using any of those before but on C# either i use Convert or (int) eg: if i wanna convert it to int32. One thing about conversion (known well as boxing and unboxing) that we always missed is that; they are costly. They (referring conversion from value type to reference type and vice versa) take a lot of resources to run and best if we could avoid from doing it (although almost impossible to avoid completely) or... try to minimize the conversion.

Remember, what differentiate all the coders are not what they programs but, how they programs :)

Congrats for the blog.
Timmy

nagakeciks berkata...

hehe...thanks timmy ... actually , i'll post something usefull / merepek , berselang seli.

In our project , we use ctype when finding a control in gridview such as dropdownlist,textbox etc, to get their value.But, i agree with you, it is costly and should try to avoid using it.

Thanks for your comment , my next post might be about dataTable , hope you can share something too here.

Thanks again to all my friends ..