Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Vs. Cast 3

Status
Not open for further replies.

dseaver

IS-IT--Management
Jul 13, 2006
467
0
0
I found when I was working on an API for a SQL db at work that I would get exceptions using a cast with Scalar type Query. I used Convert.ToInt32 instead, which solved the problem, but this is not my question.

My question is what is better practice, using a cast or using Convert.To____ to get an object into the correct type? Ever since I ran into that problem with the query, I find myself using Convert
 
I prefer to use casting where I can, because it tends to let you know exactly where the code breaks a little better.


[small]----signature below----[/small]
I can't compete with you physically, and you're no match for my brains.
You're that smart?
Let me put it this way. Have you ever heard of Plato, Aristotle, Socrates?
Yes.
Morons!
 
scalar queries return objects. they don't know what the object is. if null is returned from sql then DbNull.Value is returned from the scalar query command. you need to check what it is before casting.
Code:
int toReturn = 0;
object scalar = cmd.ExecuteScalar();
if (scalar != DbNull.Value)
{
   toReturn = (int)scalar;
}
return toReturn;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I have come to consider ExecuteScalar a time when I can't :)

I used to do almost exactly what Jason shows, until I found I could use convert, and hold off carpal tunnel a few more minutes...

[small]----signature below----[/small]
I can't compete with you physically, and you're no match for my brains.
You're that smart?
Let me put it this way. Have you ever heard of Plato, Aristotle, Socrates?
Yes.
Morons!
 
Convert to is probably faster especially if you know what you will get). and you can use try parse in most cases.

Casting will always be slow.

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Chrissie - I am very disappointed to see you here!

But thanks for the info anyways...

I got into the habit from doing things like this:

Code:
Control somControl = (Control)sender;

But apparently this is a habit I should break :)

[small]----signature below----[/small]
I can't compete with you physically, and you're no match for my brains.
You're that smart?
Let me put it this way. Have you ever heard of Plato, Aristotle, Socrates?
Yes.
Morons!
 
Are you sure Convert is faster than a direct cast? Have you benchmarked it?

I would have thought the Convert methods were just a clean wrapped implementation of a cast - therefore taking longer...

 
As far as I know casting uses reflectionBu lets do a benchmark shall we.

I did it in a better language ;-)

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim difference As Long = 0
        Dim begin As Long = 0
        Dim numberofiterations As Integer = 1000000
        Dim y As String = "10"
        Dim z As Int32 = 0
        begin = Now.Ticks
        Me.TextBox1.Text &= "Begin: " & 0 & ControlChars.CrLf
        For x As Integer = 0 To numberofiterations
            z = Convert.ToInt32(y)
        Next
        difference = Now.Ticks - begin
        Me.TextBox1.Text &= "End Convert: " & difference & ControlChars.CrLf
        begin = Now.Ticks
        For x As Integer = 0 To numberofiterations
            z = Integer.Parse(y)
        Next
        difference = Now.Ticks - begin
        Me.TextBox1.Text &= "End tryparse: " & difference & ControlChars.CrLf
        begin = Now.Ticks
        For x As Integer = 0 To numberofiterations
            z = CType(y, Int32)
        Next
        difference = Now.Ticks - begin
        Me.TextBox1.Text &= "End ctype: " & difference & ControlChars.CrLf
    End Sub

result

Code:
Begin: 0
End Convert: 2187500
End tryparse: 2031250
End ctype: 4687500
Begin: 0
End Convert: 2187500
End tryparse: 2031250
End ctype: 4531250
Begin: 0
End Convert: 2187500
End tryparse: 2031250
End ctype: 4531250

So parse is quickest then convert and twice as slow is cast.



Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Thanks Chrissie, even though your code doesn't compile and I think is making my eyes bleed.

It takes superhuman skills to work with such an inferior language ;-) Have a purple nerple.

[small]----signature below----[/small]
I can't compete with you physically, and you're no match for my brains.
You're that smart?
Let me put it this way. Have you ever heard of Plato, Aristotle, Socrates?
Yes.
Morons!
 
Thanks for all the information and input! Sticking with Converts!
 
Who forgot to give me a star ;-)

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
It's beautiful isn't it. Void of accolades.

Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Sorry 'y' would have to be an int for DirectCast to work.

If you know what you're going to convert then use DirectCast. If you don't use Parse
 
Correction 'y' would have to be an object that is an int for DirectCast to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top