views:

662

answers:

3

I usually avoid VB's built-in conversion functions (CStr, CDate, CBool, CInt, etc.) unless I need to do an actual conversion. If I'm just casting, say from an object to a string, I normally use either DirectCast or TryCast, under the assumption that CStr, etc., are doing some extra stuff I don't need. But sometimes the DirectCast syntax is a little cumbersome, as in the following example.

Dim value1 As String
Dim value2 As String
Using cn As New SqlConnection(cnStr)
    Using cmd as New SqlCommmand(sqlStr, cn)
        Using reader = cmd.ExecuteReader()
            While reader.Read()
                value1 = DirectCast(reader("COLUMN1"), String)
                value2 = CStr(reader("COLUMN1"))
            End While
        End Using
    End Using
End Using

SqlDataReader.Item returns an Object, which needs to be cast to a String. CStr is easier to read, type, and explain (IMO).

My question is, does it matter which one I use? Should I just go with CStr (and CDate and CBool, etc.) and not worry about the extra work I assume those functions are doing?

Is there any other downside to using these functions?

+1  A: 

Hi there.

In your example, you could just use:

value1 = reader("COLUMN1").ToString()

It will return the contents of the column as a string.

I always tend to favour using ToString() on an object if I can. Sometimes an object's ToString() method will return things like the class name of the object, rather then a content, so .ToString() isn't always an option.

I don't see the need for any of the VB functions CStr, CInt, etc, since the .NET framework provides plenty of good alternatives. For example.

Dim value As Integer = Convert.ToInt32(reader("Number").ToString())

Is a good way of converting a string to an int. It's worth reading up on these conversion methods, since the old VB style functions are only there for backwards compatability.

Cheers. Jas.

Jason Evans
Thanks for the comment. I used strings as an example, but the question also applies to dates, bools, ints, etc. ToString is a good suggestion for strings.
John M Gant
+4  A: 

This is a good post with discussion in the comments about DirectCast versus the CType casts and variations.

In short, if you want to be explicit about it and know what to expect, DirectCast is suggested. On the other hand, a comment by Paul Vick (VB Technical Lead) says it doesn't matter much and to just use the CType variations.

Some useful links gleaned from that post:

Ahmad Mageed
+1 Thanks for the links.
John M Gant
Paul Vick only says the speed advantage of DirectCast doesn't matter much. But another advantage of DirectCast is safety (early discovery of programming errors). Using CStr etc. indiscriminately is a step back towards `Option Strict Off`. And you don't want that.
MarkJ
A: 

Most of the time, I use CStr, CInt, CBool and CType because it's shorter and easier to read. There might be a slight performance cost but most of the time it doesn't matter. It's good to know the differences between CType, TryCast, DirectCast, and others though.

Meta-Knight