Skrim 11 Posted March 17 I get this warning, what does it mean? [dcc32 Warning] HovedUnit.pas(13295): W1036 Variable 'j' might not have been initialized J is an integer. Do I have to do, J:=0; Share this post Link to post
Remy Lebeau 1396 Posted March 17 (edited) Depends on the context. The compiler thinks you are using J's value before you have actually assigned any value to J. Sometimes the compile makes a mistake on this, sometimes not, so it may or may not be a false positive. How are you actually using J exactly? Can you show that code? Edited March 17 by Remy Lebeau Share this post Link to post
Lars Fosdal 1792 Posted March 18 Frequent scenario function Test: integer; var i: integer; begin try i := 1; // do something finally; Result := i; end; end; i needs to be initialized before the try block. 2 Share this post Link to post
Skrim 11 Posted March 18 On 3/17/2024 at 5:31 PM, Remy Lebeau said: Depends on the context. The compiler thinks you are using J's value before you have actually assigned any value to J. Sometimes the compile makes a mistake on this, sometimes not, so it may or may not be a false positive. How are you actually using J exactly? Can you show that code? Here is my code, also could it be done easier/more elegant // String 000001203 to 1203 // Numbers of zeros to the left could be 1..n function RemoveZeroLeft(var s : String) : string; var i, j : integer; begin for i:=1 to length(s)do begin if s<>#48 then begin j:=i; break; end; end; result:=copy(s,j,length(s)); end; Share this post Link to post
Stano 143 Posted March 18 If the condition "if s<>#48 then" is never satisfied, then j is not initialized. immediately after begin, initialize the variable. I use inline var for this. 2 Share this post Link to post
Remy Lebeau 1396 Posted March 19 (edited) I would opt to simply eliminate J altogether: function IndexOfFirstNotOf(const s: string; Ch: Char): Integer; begin for Result := 1 to Length(s) do begin if s[Result] <> Ch then Exit; end; Result := 0; end; function RemoveZeroLeft(const s : String) : string; var i : integer; begin i = IndexOfFirstNotOf(s, '0'); if i > 1 then Result := Copy(s, 1, MaxInt) else if i = 1 then Result := s else Result := ''; // or '0' if needed end; Edited March 19 by Remy Lebeau Share this post Link to post
Stefan Glienke 2002 Posted March 19 https://docwiki.embarcadero.com/Libraries/Athens/en/System.SysUtils.TStringHelper.TrimLeft 3 Share this post Link to post