Conversions... The Right Way? [VB]

MTSkull

Centurion
Joined
Mar 25, 2003
Messages
151
Location
Boulder, Colorado
I am parsing a string and then converting it to a Date Time. Here is the code I use, is this the best way or is there a better way.

Visual Basic:
dim tempStr as string
dim tempArr() as string
Dim fileDateTime As Date

'fill tempArr with data from a text file

'Build the Date of the File
tempStr = tempArr(3).Substring(0, 2) & "/"
tempStr &= tempArr(3).Substring(2, 2) & "/"
tempStr &= tempArr(3).Substring(4, 4)

'Build the time of the file
tempStr &= " " & tempArr(4).Substring(0, 2) & ":"
tempStr &= tempArr(4).Substring(2, 2) & ":"
tempStr &= tempArr(4).Substring(4, 2)

fileDateTime = fileDateTime.Parse(tempStr)

What I am unsure of is the last line. Using the parse method to fill fileDateTime variable with the constructed date string.

Thanks
MTS
 
Thanks,
I searched this forum for how to do conversions because I still use cint, cstr, etc. I ran across conversions in anouther post and did not know about parse, cType, or the convert class. When should one use these differnet methods? Or just point me to an artical so I can read about it. The .net help hints at why but does not compare them directly.

Thanks
MTS
 
Many VB.NET MSDN examples use CInt and CStr to convert,
but it's better practice to use the static methods of the Convert
class or the Parse methods of the types (like you used) to convert,
because it makes the code independent from language-specific
features. It also makes it easier to read for other .NET coders,
and makes the code more easily ported to other .NET languages
(if you had the urge).

As for casting between different reference types, use DirectCast
instead of CType.
 
Thanks Bucky (any relation to Bucky Katt?)

This program uses a vb.net Template I wrote in July. Since then I have learned a lot of new stuff about vb.net that i would like to go back and fix in the template. Due to time constraints that won't be possible, so this program has a lot of legacy calls as well as newer .net stuff.

I really like the data object model. I love Datasets and stuff. The connection to SQL server is very nice. What I have used so far.
 
Back
Top