0

I have a code. In this code I have a label1 with text: "0,5 min", from substring I get "0,5" and now I need to convert it from minutes to milliseconds like 0,5 * 60 * 1000 = 30000. I can't do it because of this error:

Input string was not in a correct format.

How can I do it?

private async void button2_Click(object sender, EventArgs e)
{
    string num = label1.Text.Substring(0, label1.Text.IndexOf(" min"));
    int mul = Convert.ToInt32(num) * 60 * 1000;

    textBox1.Text = $"{num} - {mul}";
    await Task.Delay(mul);
}
  • You should either use a dot as decimal-seperator, or provide your locale-settings to [`Convert.ToInt32`](https://learn.microsoft.com/dotnet/api/system.convert.toint32?view=net-6.0#system-convert-toint32(system-string-system-iformatprovider)) – MakePeaceGreatAgain Mar 03 '22 at 10:47
  • 2
    Apart from this, `0.5` surely is not an integer, but a decimal, isn't it? – MakePeaceGreatAgain Mar 03 '22 at 10:48
  • @MakePeaceGreatAgain I don't know, that's why I'm asking for help – JustChickNugget Mar 03 '22 at 10:50
  • Why are you scraping that value off of a Label and not from a class field, which both informs the Label's Text _and_ the conversion? – Fildor Mar 03 '22 at 10:53
  • 1
    @MakePeaceGreatAgain OP shouldn't use `Convert` _at all_. – Fildor Mar 03 '22 at 10:54
  • @MakePeaceGreatAgain I got my problem. I used integer not double! – JustChickNugget Mar 03 '22 at 10:56
  • 1
    @Fildor I entirely agree - Microsoft really needs to add `[Obsolete]` to the `Convert` class, and it should have done so over a decade ago. – Dai Mar 03 '22 at 11:01
  • 1
    Reading this again, I think this is actually an x-y question. That value should not be taken from a label's Text property in the first place. It's a design smell. – Fildor Mar 03 '22 at 11:26

1 Answers1

0

Depending on the exact error you're getting, you might want to replace , with . (though ideally specify an explicit CultureInfo).

Then you can use double.TryParse(num, out value);

Also you might want to change it from int to double or decimal (use decimal if you need precision, as double and float are inexact, approximate, types).

Dai
  • 141,631
  • 28
  • 261
  • 374
shaked428
  • 56
  • 6
  • 2
    I won't dv, but I cannot stress this enough. The whole ordeal is unneccessary. OP should use a backing model, so that conversion is completely unneccessary. The label's Text property should then be bound to reflect that model's Value. Changes should be made to the model. And the delay in the question's click handler should use the model's value. For example a `TimeSpan` would be an excellent choice. – Fildor Mar 03 '22 at 11:21
  • @Fildor WinForms' main strength is RAD though - going through the trouble of defining an internal domain model to mediate between the UI and whatever else is there would be considered _added and unnecessary complexity_ depending on the business requirements (i.e. get something-that-will-never-be-updated out the door asap) - not to mention the headaches people have with WinForm's anemic (if not broken) built-in databinding support - the kind of headaches that make people start-over with Electron because things are _just that bad_ now. – Dai Mar 03 '22 at 13:06
  • @Dai Agreed. And then you end up scraping values off of some label's text ... :/ – Fildor Mar 03 '22 at 13:38
  • 1
    @Fildor Did you agree to disagree? :) I cannot see how adding a simple Model binder that implements `INotifyPropertyChanged` is *added and unnecessary complexity* as @Dai mentioned. That's standard / every-day implementation in this platform (as *in the other* platform). The *added complexity* is the (strings parsing + locale handling) shenanigans. That's what the OP should really avoid. – Jimi Mar 03 '22 at 14:49
  • 1
    @Jimi I agreed to "WinForms' main strength is RAD". But yes, I kind of disagreed, too. He is kind of right in that binding is a little pain in the rear in WinForms. But the alternatives are mostly much worse as seen in the question. So, in the end it depends what you define as "added and unneccessary complexity". – Fildor Mar 03 '22 at 15:07