MTSkull Posted December 29, 2003 Posted December 29, 2003 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. 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 Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
*Experts* Bucky Posted December 29, 2003 *Experts* Posted December 29, 2003 Using Parse is the correct way to do it. It looks fine. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
MTSkull Posted December 29, 2003 Author Posted December 29, 2003 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 Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
*Experts* Bucky Posted December 30, 2003 *Experts* Posted December 30, 2003 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
MTSkull Posted December 30, 2003 Author Posted December 30, 2003 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. Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
*Experts* Bucky Posted December 31, 2003 *Experts* Posted December 31, 2003 Hey, no problem. My name is actually a reference to R. Buckminster Fuller, although Get Fuzzy is one of my favorite comics. :) Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.