Solved

Parsing Combobox.Text to use in a TimeSpan - How?

Posted on 2006-06-17
9
486 Views
Last Modified: 2006-11-18

Hello Experts,

Here's what I have and it works great!

Private dtm_Time_Preset As TimeSpan = TimeSpan.Parse("00:00:05")

Here's what I want and it doesn't work at all!

Private dtm_Time_Preset As TimeSpan = TimeSpan.Parse(cbo_Preset.Text)

Thankx for any help!
0
Comment
Question by:donniedarko801
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 450 total points
ID: 16927236
Hi donniedarko801;

I suspect that the problem you are having is that the controls have not been initialized yet on the form and you are getting an error such as

An unhandled exception of type 'System.NullReferenceException' occurred in the application.  Object reference not set to an instance of an object

The reason is that a Class level variable is initialized before the New subroutine and non of the controls have been created. to correct your problem do the following.

    Private dtm_Time_Preset As TimeSpan

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        dtm_Time_Preset = TimeSpan.Parse(cbo_Preset.Text)

    End Sub

I hope that this is of some help.

Fernando

0
 
LVL 14

Assisted Solution

by:ptakja
ptakja earned 50 total points
ID: 16927240
What version of VS are you using? I just put this code into VS2005 and it works fine:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dtm_Time_Preset As TimeSpan = TimeSpan.Parse(ComboBox1.Text)
        MessageBox.Show(dtm_Time_Preset.Seconds.ToString)
    End Sub

Combobox has several preset timespans in it that look like this:

00:00:05
00:00:10
00:00:15
00:00:30

etc...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 16927257
Hi ptakja;

Your code works fine because the control has already been created. The author of the question is using a Private declaration which can not be used in as a local variable in a sub.

see my post.

Fernando
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:donniedarko801
ID: 16927343
Thx Fernando.

By the way, could this approach ever work? Because it doesn't...

dtm_Time_Preset = TimeSpan.Parse(cbo_Hr & ":" & cbo_Min & ":" & "00")
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 16927366
If cbo_Hr and cbo_Min are ComboBox object then you need to reference the text box of the control to get the string like this:

dtm_Time_Preset = TimeSpan.Parse(cbo_Hr.Text & ":" & cbo_Min.Text & ":" & "00")
0
 
LVL 1

Author Comment

by:donniedarko801
ID: 16927379
Genious!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 16927387
No far from that, LOL :=)
0
 
LVL 14

Expert Comment

by:ptakja
ID: 16927460
Fernando, I didn't see your post when I posted mine. But you are absolutely right on.

Personally, I prefer to use the String.Format method instead of concatinating strings using "&" all over the place. So I would code the line in the post above like this:

dtm_Time_Preset = TimeSpan.Parse(String.Format("{0}:{1}:00", cbo_Hr.Text, cbo_Min.Text))

I find this format much easier to read and less prone to error with missing quotes, especially for SQL statements!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 16927493
ptakja; I agree with that the format method is much neater.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question