Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

  • *Experts*
Posted
Using Parse is the correct way to do it. It looks fine.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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.

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

  • *Experts*
Posted

Hey, no problem. My name is actually a reference to R. Buckminster

Fuller, although Get Fuzzy is one of my favorite comics. :)

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...