Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable might not have been initialized. Can I switch this warning on for a string?

When I compile this code

{$WARNINGS ON}
function Test(s: string): string;
var
  t: string;
  d: double;
begin
  if s = '' then begin
    t := 'abc';
    d := 1;
  end;

  Result := t + FloatToStr(d);
end;

I get the warning "Variable 'd' might not have been initialized", but I do not get the same warning for variable 't'. This seems inconsistent. This code is only a simple example to show the compiler warnings, but I have just found a bug in my live code which would have been caught by a compile-time warning for uninitialised string variables. Can I switch this warning on somehow in Delphi 6? Or in a newer version of Delphi?

like image 642
soid Avatar asked Apr 24 '11 13:04

soid


People also ask

What happens if you use a variable that has not been initialized?

Initializing a variable means specifying an initial value to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.

What happens if you don't initialize a variable Java?

If you declare a variable as final, it is mandatory to initialize it before the end of the constructor. If you don't you will get a compilation error.

What happens if you attempt to use a variable before it has been initialized Java?

Local Variables If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.


1 Answers

Nope, there is no switch for this. The warning doesn't occur because a string is a compiler managed type and is always initialized by the compiler.

like image 83
Marjan Venema Avatar answered Nov 08 '22 11:11

Marjan Venema