357mag 2 Posted April 28, 2023 I've got a program that asks the user to enter a value (in an Edit box) and then when he clicks on the Show Value button, the Label should say "The value of x is " plus whatever value he entered in the Edit box. But Delphi is complaining saying "x is not a valid integer value." I don't know what's wrong with my code but here is what I've got: var FormSimpleVariable: TFormSimpleVariable; x : integer; implementation {$R *.dfm} procedure TFormSimpleVariable.ButtonQuitClick(Sender: TObject); begin Application.Terminate; end; procedure TFormSimpleVariable.ButtonShowValueClick(Sender: TObject); begin x := StrToInt('x'); LabelResult.Caption := 'The value of x is ' + IntToStr(x); end; end. Share this post Link to post
Remy Lebeau 1393 Posted April 28, 2023 (edited) Do you understand what StrToInt() actually does? What do you think StrToInt('x') should do, and why do you think it should NOT throw an error (hint: it SHOULD)? The string 'x' is not representative of a valid integer value, so of course it can't be converted into an actual integer. You said the user is entering a value in an Edit box, but this code is not accessing that Edit box at all. You need something more like this instead: procedure TFormSimpleVariable.ButtonShowValueClick(Sender: TObject); var x: Integer; begin x := StrToInt(EditValue.Text); LabelResult.Caption := 'The value of x is ' + IntToStr(x); end; Alternatively, for numeric input, consider using a more appropriate UI control, like TSpinEdit or TNumberBox, which provide the input as actual integers rather than as strings. Edited April 28, 2023 by Remy Lebeau Share this post Link to post
programmerdelphi2k 237 Posted April 28, 2023 considere (too) alternative "Def" function to: StrToxxxDef(..., ...) Quote StrToIntDef('your number here', 0); StrToInt64Def('your number here', 0); StrToFloat() StrToUInt64() StrToUInt64Def() StrToFloatDef() Share this post Link to post
David Heffernan 2345 Posted April 28, 2023 I had to read this question x times before I understood it 1 Share this post Link to post
357mag 2 Posted April 28, 2023 How come you can't assign a value to a variable in the same section you declared it? You gotta assign the value underneath the begin keyword instead. Share this post Link to post
programmerdelphi2k 237 Posted April 28, 2023 (edited) 1 hour ago, David Heffernan said: before I understood it your CPU it's very fast... there is a delay time... it's ok! 😂 ...1, 2, 3, 5, 8... ops! starting again... bip! bip! Edited April 28, 2023 by programmerdelphi2k Share this post Link to post
Attila Kovacs 629 Posted April 28, 2023 Are you Roman? What do you expect from "StrToInt('x')" ? 10 ? 1 Share this post Link to post
357mag 2 Posted April 28, 2023 You guys are acting like jerks. I'm talking about a different piece of code now. If you can't or don't know how to be nice and respectful, then please go to bed. Share this post Link to post
Attila Kovacs 629 Posted April 28, 2023 49 minutes ago, 357mag said: How come you can't assign a value to a variable in the same section you declared it? You gotta assign the value underneath the begin keyword instead. where do you assign it in c++/pick one ? before the "{"/pick one ? Share this post Link to post
Remy Lebeau 1393 Posted April 28, 2023 5 hours ago, programmerdelphi2k said: considere (too) alternative "Def" function to: StrToxxxDef(..., ...) And alternatively, the TryStrToxxx() functions. Share this post Link to post
programmerdelphi2k 237 Posted April 29, 2023 @Remy Lebeau right! but "StrToxxxxxxDEF(....)" it's more flexible (it dont needs a "var" to receive "out param...") var v: integer; // mandatory to TryStrToxxxx(...) begin TryStrToInt('x', v); // more conservadore (* function TryStrToInt(const S: string; out Value: Integer): Boolean; var E: Integer; begin Val(S, Value, E); Result := E = 0; // exchanging 6 for half a dozen end; *) // StrToIntDef('x', 0); // more flexible (* function StrToIntDef(const S: string; Default: Integer): Integer; var E: Integer; begin Val(S, Result, E); if E <> 0 then Result := Default; // exchanging 6 for half a dozen end; *) // at end, if works it's good too! Share this post Link to post
357mag 2 Posted April 29, 2023 5 hours ago, Attila Kovacs said: where do you assign it in c++/pick one ? before the "{"/pick one ? 5 hours ago, Attila Kovacs said: where do you assign it in c++/pick one ? before the "{"/pick one ? You guys are acting like jerks. I'm talking about a different piece of code now. If you can't or don't know how to be nice and respectful, then please go to bed. In C++ you can assign a value to a variable immediately after declaring it. Delphi would not let me do that. I had to assign the values to the variables further down underneath the begin keyword. 1 Share this post Link to post
David Heffernan 2345 Posted April 29, 2023 1 hour ago, 357mag said: In C++ you can assign a value to a variable immediately after declaring it. Delphi would not let me do that. I had to assign the values to the variables further down underneath the begin keyword. This is not true. You can use an inline variable declaration. https://docwiki.embarcadero.com/RADStudio/en/Inline_Variable_Declaration Please don't tell us we are jerks. That's just offensive and rude. 3 Share this post Link to post
Attila Kovacs 629 Posted April 29, 2023 4 hours ago, 357mag said: I had to assign the values to the variables further down underneath the begin keyword. In c++, you are also assigning it after the "begin". Share this post Link to post
aehimself 396 Posted April 29, 2023 If StrToInt says a string is not a number thus it can not be converted, trust it, it's true. If you are really interested, you can use the function "Val" which will even tell you which character is causing it to fail. You might want to sanitize your input first for spaces or other non-printable characters which get there easily if you copy-paste from documents / websites. Also, consider what @Remy Lebeau said, and use a TSpinEdit or TNumberBox instead (you can check NumbersOnly in TEdit as well). This way the input won't even be accepted if the pasted string / typed character is not a number. Share this post Link to post
David Heffernan 2345 Posted April 29, 2023 I'm guessing what happened here is that asker put the variable name into a string and imagined it would be interpreted in the context of the variable namespace. Share this post Link to post
Remy Lebeau 1393 Posted April 29, 2023 19 hours ago, programmerdelphi2k said: but "StrToxxxxxxDEF(....)" it's more flexible (it dont needs a "var" to receive "out param...") On the other hand, the StrToxxxDef() functions don't let you differentiate whether valid vs invalid input was entered unless you use a sentinel value that can't appear in any valid input. The TryStrToxxx() functions let you differentiate very easily. Besides, what is the point of converting a string to an integer/floating type if you are not going to use the converted value anyway? var v: integer; begin // don't ignore the return value! // TryStrToInt('...', v); if TryStrToInt('...', v) then begin // use v as needed ... end else begin // error converting ... end; // don't ignore the return value! // StrToIntDef('...', 0); v := StrToIntDef('...', 0); if v <> 0 then begin // use v as needed ... end else begin // was the input actually '0', or did an error occur ??? end; end; Share this post Link to post
Remy Lebeau 1393 Posted April 29, 2023 59 minutes ago, aehimself said: Also, consider what @Remy Lebeau said, and use a TSpinEdit or TNumberBox instead (you can check NumbersOnly in TEdit as well). This way the input won't even be accepted if the pasted string / typed character is not a number. Do note that setting TEdit.NumbersOnly=true merely tells the TEdit to accept only digit characters and no others (so, no negatives, no decimals, etc) , but it will not validate that the text as a whole will convert to a valid integer. So you still have to handle that conversion yourself, if you don't use a UI control that does it for you. Share this post Link to post
programmerdelphi2k 237 Posted April 29, 2023 34 minutes ago, Remy Lebeau said: if you are not going to use the converted value anyway? The excence of the alternative functions in question is the same! Val(S, Value, E); What distinguishes the two is just how the return is reported to the user: returns a boolean; the other an integer; however, the "out" parameter has the same function: capture the value, a value "pre-defined" by the user, and use it: either through a variable informed by the user, or through the return of the function. they are two flavors of the same fruit! // none error at all, just "is valid or not?" var LResult: integer; begin if StrToIntDef('hello', 0) = 0 then ShowMessage('bye bye'); // LResult := StrToIntDef('hello', 0); // if (LResult = 0) then ShowMessage('bye bye'); // // if TryStrToInt('hello', LResult) then { ... }; // if (LResult = 0) then ShowMessage('bye bye'); Share this post Link to post
David Heffernan 2345 Posted April 29, 2023 The point about return value vs out param is that the former can be composed directly in chained function calls. Share this post Link to post
programmerdelphi2k 237 Posted April 29, 2023 if TryStrToInt('hello', LResult) and (LResult >0) then ShowMessage(' it,s good for us :) '); Share this post Link to post
357mag 2 Posted May 2, 2023 I don't recall adding those single quotes around x (like ('x')). Don't know what happened there. Mistakes and being human I guess. Share this post Link to post
Virgo 18 Posted May 3, 2023 7 hours ago, 357mag said: I don't recall adding those single quotes around x (like ('x')). Don't know what happened there. Mistakes and being human I guess. Without those quotes the code would not compile... Share this post Link to post
microtronx 38 Posted May 3, 2023 On 4/28/2023 at 7:10 PM, 357mag said: I've got a program that asks the user to enter a value (in an Edit box) and then when he clicks on the Show Value button, the Label should say "The value of x is " plus whatever value he entered in the Edit box. But Delphi is complaining saying "x is not a valid integer value." I don't know what's wrong with my code but here is what I've got: var FormSimpleVariable: TFormSimpleVariable; x : integer; implementation {$R *.dfm} procedure TFormSimpleVariable.ButtonQuitClick(Sender: TObject); begin Application.Terminate; end; procedure TFormSimpleVariable.ButtonShowValueClick(Sender: TObject); begin x := StrToInt('x'); LabelResult.Caption := 'The value of x is ' + IntToStr(x); end; end. simply change your line from x := StrToInt('x'); to x := StrToInt(inttostr(x)); crazy, but will work. But I do not understand why someone would do this Share this post Link to post