[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-06-17
9
Medium Priority
?
493 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
  • 5
  • 2
  • 2
9 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1800 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 200 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 64

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 64

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 64

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 64

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

834 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