Link to home
Start Free TrialLog in
Avatar of donniedarko801
donniedarko801

asked on

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


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!
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of donniedarko801
donniedarko801

ASKER

Thx Fernando.

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

dtm_Time_Preset = TimeSpan.Parse(cbo_Hr & ":" & cbo_Min & ":" & "00")
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")
Genious!
No far from that, LOL :=)
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!
ptakja; I agree with that the format method is much neater.