TimeSpan.Parse resulting in 00:00:00, why?

  • Thread starter Thread starter ThunderMusic
  • Start date Start date
T

ThunderMusic

Hi,
I'm using the timespan's Parse method to build my timespan from a
string. here's the line I use :

m_Delay.Parse(RegKey.GetValue("Delay", "01:00:00"))

the value I get from the RegKey is 00:00:30, so it should give a timespan of
30 seconds, but when I do msgbox(m_Delay.ToString), it displays 00:00:00.
Am I doing something wrong?

thanks
 
ThunderMusic,
| m_Delay.Parse(RegKey.GetValue("Delay", "01:00:00"))

TimeSpan.Parse is a shared method that returns a new TimeSpan value.

TimeSpan itself is immutable (doesn't change).

Try something like:

m_Delay = TimeSpan.Parse(RegKey.GetValue("Delay", "01:00:00"))

Hope this helps
Jay

| Hi,
| I'm using the timespan's Parse method to build my timespan from a
| string. here's the line I use :
|
| m_Delay.Parse(RegKey.GetValue("Delay", "01:00:00"))
|
| the value I get from the RegKey is 00:00:30, so it should give a timespan
of
| 30 seconds, but when I do msgbox(m_Delay.ToString), it displays 00:00:00.
| Am I doing something wrong?
|
| thanks
|
|
 
I found it too. I had not seen the method was shared (static), so I juste
had to do the following :

m_Delay = TimeSpan.Parse(RegKey.GetValue("Delay", "01:00:00"))

Now it works fine.
 
Jay B. Harlow said:
| m_Delay.Parse(RegKey.GetValue("Delay", "01:00:00"))

TimeSpan.Parse is a shared method that returns a new TimeSpan value.

TimeSpan itself is immutable (doesn't change).

Try something like:

m_Delay = TimeSpan.Parse(RegKey.GetValue("Delay", "01:00:00"))

Just one example of why static (shared) methods shouldn't be callable
as if they were instance methods...
 
Jon,
| Just one example of why static (shared) methods shouldn't be callable
| as if they were instance methods...
I couldn't agree more. Fortunately VB.NET 2005 warns you when you attempt to
call a shared (static) method as an instance method.

Jay

| > | m_Delay.Parse(RegKey.GetValue("Delay", "01:00:00"))
| >
| > TimeSpan.Parse is a shared method that returns a new TimeSpan value.
| >
| > TimeSpan itself is immutable (doesn't change).
| >
| > Try something like:
| >
| > m_Delay = TimeSpan.Parse(RegKey.GetValue("Delay", "01:00:00"))
|
| Just one example of why static (shared) methods shouldn't be callable
| as if they were instance methods...
|
| --
| Jon Skeet - <[email protected]>
| http://www.pobox.com/~skeet
| If replying to the group, please do not mail me too
 
I couldn't agree more. Fortunately VB.NET 2005 warns you when you attempt
to
call a shared (static) method as an instance method.
And that is in my opinion the right approach. (I don't like the word
shouldn't). Although I see not any reason why it shouldn't. (I can see a
reason however that is against my way of writing a program and that is when
in the static/shared procedure a static/value is set).

:-)

Cor
 
And that is in my opinion the right approach. (I don't like the word
shouldn't). Although I see not any reason why it shouldn't. (I can see a
reason however that is against my way of writing a program and that is when
in the static/shared procedure a static/value is set).

The reason you shouldn't (and I'm not afraid to use that word) is that
it gives the wrong impression. It gives the impression that the method
you're calling is related to the instance you appear to be calling it
on, when that simply isn't the case.

For instance, if you create a new Thread and then appear to call Sleep
on it, it *looks* like you're somehow telling the other thread to
sleep, when in fact it's making the current thread sleep.

Things which harm readability in this way really shouldn't be used.
Personally I think it would be better as an error than as a warning,
but I can see why changing it to an error at this stage would be a real
problem.

Of course, it's worth always trying to write code which doesn't
generate warnings anyway, and if you do that it doesn't really matter
whether it's a warning or an error...
 
Jon,
| Things which harm readability in this way really shouldn't be used.
| Personally I think it would be better as an error than as a warning,
| but I can see why changing it to an error at this stage would be a real
| problem.
I just double checked. It defaults to a warning for new projects; using the
project properties you can change it to either: None, Warning, or Error.

FWIW: There are a handful of other compile messages, such as "unused local
variable" or "recursive operator & property access", that you can set to
None, Warning, or Error which is rather nice.

Jay

| > > I couldn't agree more. Fortunately VB.NET 2005 warns you when you
attempt
| > > to call a shared (static) method as an instance method.
|
| > And that is in my opinion the right approach. (I don't like the word
| > shouldn't). Although I see not any reason why it shouldn't. (I can see a
| > reason however that is against my way of writing a program and that is
when
| > in the static/shared procedure a static/value is set).
|
| The reason you shouldn't (and I'm not afraid to use that word) is that
| it gives the wrong impression. It gives the impression that the method
| you're calling is related to the instance you appear to be calling it
| on, when that simply isn't the case.
|
| For instance, if you create a new Thread and then appear to call Sleep
| on it, it *looks* like you're somehow telling the other thread to
| sleep, when in fact it's making the current thread sleep.
|
| Things which harm readability in this way really shouldn't be used.
| Personally I think it would be better as an error than as a warning,
| but I can see why changing it to an error at this stage would be a real
| problem.
|
| Of course, it's worth always trying to write code which doesn't
| generate warnings anyway, and if you do that it doesn't really matter
| whether it's a warning or an error...
|
| --
| Jon Skeet - <[email protected]>
| http://www.pobox.com/~skeet
| If replying to the group, please do not mail me too
 
Jay B. Harlow said:
| Things which harm readability in this way really shouldn't be used.
| Personally I think it would be better as an error than as a warning,
| but I can see why changing it to an error at this stage would be a real
| problem.
I just double checked. It defaults to a warning for new projects; using the
project properties you can change it to either: None, Warning, or Error.
Excellent.

FWIW: There are a handful of other compile messages, such as "unused local
variable" or "recursive operator & property access", that you can set to
None, Warning, or Error which is rather nice.

Very nice. I'm doing Java development as well as C# at the moment,
which means I'm being spoiled by how nice the Eclipse compiler is. It
has all kinds of optional errors/warnings. I'm being spoiled by Eclipse
in all kinds of other ways, too, but that's a different story.
 
Back
Top