13 Replies - 4257 Views - Last Post: 02 February 2012 - 03:35 PM Rate Topic: -----

#1 slyfuldragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 01:35 PM

Working on a personal project.
Originally had the file loaded had the program read a number and had trouble converting that number of total seconds into a TimeSpan.

Finally got it working with a simple code, after COUNTLESS tried of different code. lol:
   Dim nmTime As TimeSpan = TimeSpan.FromSeconds(TextBox1.Text)
        TextBox1.Text = nmTime.ToString



Now I'm stuck converting that Timespan (in the DD:HH:MM:SS format) back to a integer of total seconds.
Tried a bunch of codes I could think of but it was time that I had a fresh idea from other people. Any help is much appreciated.

Thanks guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Converting Timespan string back to integer

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9580
  • View blog
  • Posts: 36,300
  • Joined: 12-June 08

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 01:43 PM

Wow.. that seems pretty convoluted. Why did you not use the constructor for timespan, fill in a zero for hour.. a zero for minute.. and your seconds into it?

        Dim lSeconds As Int32 = 12

        Dim tsFoo As New TimeSpan(0, 0, lSeconds) '-- hour, minute, seconds
        Console.WriteLine(tsFoo.TotalSeconds.ToString)

        Dim lSecondsReturn As Int32 = 0
        lSecondsReturn = tsFoo.TotalSeconds

        Console.WriteLine(lSecondsReturn)


Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 01:43 PM

So what do you have in textbox1.text, I'm just trying to get a grasp of what you're doing and what you want as an end result.

The timespan has a .TotalSeconds property but I'm not sure that's what you're actually after.
Was This Post Helpful? 0
  • +
  • -

#4 slyfuldragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 01:57 PM

View PostCharlieMay, on 02 February 2012 - 03:43 PM, said:

So what do you have in textbox1.text, I'm just trying to get a grasp of what you're doing and what you want as an end result.

The timespan has a .TotalSeconds property but I'm not sure that's what you're actually after.


Sorry,
Basically I have a file that I load in the program that looks at a particular and set location within that file for a number. That number/integer (Total seconds) is then converted into a timespan string format into a textbox.

I'm trying to get it to where if a person edits that textbox in the timespan format and saves the file, it converts the timespan string format back to the number/integer (Total seconds).

View Postmodi123_1, on 02 February 2012 - 03:43 PM, said:

Wow.. that seems pretty convoluted. Why did you not use the constructor for timespan, fill in a zero for hour.. a zero for minute.. and your seconds into it?

        Dim lSeconds As Int32 = 12

        Dim tsFoo As New TimeSpan(0, 0, lSeconds) '-- hour, minute, seconds
        Console.WriteLine(tsFoo.TotalSeconds.ToString)

        Dim lSecondsReturn As Int32 = 0
        lSecondsReturn = tsFoo.TotalSeconds

        Console.WriteLine(lSecondsReturn)



Well I don't have any problems with the number/integer converting into the timespan string format. but the other way around when I go to save the file.

This post has been edited by slyfuldragon: 02 February 2012 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9580
  • View blog
  • Posts: 36,300
  • Joined: 12-June 08

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 02:05 PM

Yeah except you go about it in an odd fashion. I was showing both ways are easy-peasy to do.
Was This Post Helpful? 0
  • +
  • -

#6 slyfuldragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 02:08 PM

View Postmodi123_1, on 02 February 2012 - 04:05 PM, said:

Yeah except you go about it in an odd fashion. I was showing both ways are easy-peasy to do.


oooooooh lol, ok gotcha.
Other ways are good to know.
Was This Post Helpful? 0
  • +
  • -

#7 slyfuldragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 02:32 PM

In other words This is what I have when the program reads the file in Hex:

        Reader.Position = &HF122
        TextBox1.Text = Reader.ReadInt32
        Reader.Close()

        Dim nmTime As TimeSpan = TimeSpan.FromSeconds(TextBox1.Text)
        TextBox1.Text = nmTime.ToString



at that location the numeric number is 69149 (total seconds) which shows the user "19:12:29" (19 hours, 12 mins, and 29 second) which is correct

I'm trying to get where the time format will convert back to a numeric number if/when the user makes changes to that time format using something with the write function

        Writer.Position = &HF122
        Writer.WriteInt32(TextBox1.Text)



Hope that helps clear some things up of what exactly I'm trying to do.

This post has been edited by slyfuldragon: 02 February 2012 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 03:03 PM

I guess you could parse through each section and multiply by the number of seconds for each and add them together to get your result

Split the string at the : and then multiply the number of days by 86400, the number of hours * 3600 etc... then add those values together to get the total seconds. There is probably a method built into .Net but I don't know what it is.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9580
  • View blog
  • Posts: 36,300
  • Joined: 12-June 08

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 03:11 PM

Timespan's "TotalSeconds" does that already. If I create a timespan with 0h, 1m, 12s that method gives me back 72s
Was This Post Helpful? 1
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 03:16 PM

CharlieMay Why are you over complicating things?
String spliting?. Strings that are digits aren't numbers.
Why reinvent the wheel? The wheel has be invented, all you can do is re-imagine it.
Use the stuff already provided.
  • Textbox contents have changed (an event you can react to)
  • TimeSpan has methods that attempt to convert text(string) in to time encapsulation.
  • When you have a valid parsing. Raise another event to say you've got new one.


or use (or create) a control that is time (not text).

This post has been edited by AdamSpeight2008: 02 February 2012 - 03:20 PM

Was This Post Helpful? 1
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 03:16 PM

Using his code above I can see where nmTime.TotalSeconds would display total seconds involved. But his question and I guess mine now too is how to you take the string in the textbox "00:01:12" and create a timespan so that you can use totalseconds?
Was This Post Helpful? 0
  • +
  • -

#12 slyfuldragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 03:20 PM

View PostCharlieMay, on 02 February 2012 - 05:16 PM, said:

Using his code above I can see where nmTime.TotalSeconds would display total seconds involved. But his question and I guess mine now too is how to you take the string in the textbox "00:01:12" and create a timespan so that you can use totalseconds?


Exactly, yes.
Sorry guys for the mass confusion.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 03:22 PM

It'll help if you read the {beep} documentation available on TimeSpan.

TimeSpan.TryParse
TimeSpan.TryParseExact
Was This Post Helpful? 1
  • +
  • -

#14 slyfuldragon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Problem with Converting Timespan string back to integer

Posted 02 February 2012 - 03:35 PM

Regardless, Thanks guys. I pretty much have/had programer's block and needed others views since I've been going at it for a while without breaks. Going to take a small break, do some more reading with this 2 links provided and then get back into it.

This post has been edited by slyfuldragon: 02 February 2012 - 03:36 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1