Using with clause and local variable

Hi
I had this code working in older public release, but now I get warning “Local variable “fname” is assigned to but never read”. Is this correct? I use e.g. .NET console app with Delphi compatibility and legacy with clause support :

namespace ConsoleApplication3;

type
  
  TC = class
  private
    fname : String ;        
  public
    property FieldName : String read fname write fname;
  end;
  
  Program = class
  public
    class method Main(args: array of String): Int32;
    begin    
      var c := new TC();          
      var fname := 'x';  // <- here
      with c do begin
        if fname <> FieldName then
          FieldName := fname ;
      end ;
    end;
  end;
end.

fname is a local variable but inside with clause, it becomes a class variable. The key here is using the same variable name. Is this a bug or correct code in the latest public release?

Seems legit.

in Delphi compatibility, private means unit, so with c brings the “private” c.fname into scope and hides the local fname variable. the warning is correct, the local is never read — and the warning in fact notifies you that your code does not behave as you thought it would :).

this is why both Delphi’s visibility model and legacy with are evil and especially the later shield never, ever be used :wink:

You should also have the warning that fName is hiding a variable.

with doesn’t generate warnings for hiding stuff from the local scope, never did. there’d be far too much stuff to check, since with always hides something (even if only ToString ;).