Delphi SerAndDeser.pas(298):W1036变量“tForm”可能尚未初始化

Delphi SerAndDeser.pas(298):W1036变量“tForm”可能尚未初始化,delphi,variables,build,warnings,delphi-xe2,Delphi,Variables,Build,Warnings,Delphi Xe2,有一个密码 for j := 0 to mForm.ComponentCount - 1 do if mForm.Components[j] is TableFormInfo then //try //table := nil; //tempFmtable := nil; //tForm := nil; tForm := mForm.Components[j] as TableFormInfo;

有一个密码

for j := 0 to mForm.ComponentCount - 1 do
    if mForm.Components[j] is TableFormInfo then
      //try
        //table := nil;
        //tempFmtable := nil;
        //tForm := nil;

        tForm := mForm.Components[j] as TableFormInfo;

        table := TTableSpec(DBSchema.Tables.FindComponent(tForm.Table));
        tempFmtable := TfmTableData.Create(MainWindow);
        tempFmtable.Name := tForm.Name;
        tempFmtable.tname := tForm.Table;
        //tempFmtable.Caption := Utf8ToAnsi(table.Description);
        tempFmtable.Left := tForm.LeftValue;
        tempFmtable.Top := tForm.TopValue;
        tempFmtable.Height := tForm.HeightValue;
        tempFmtable.Width := tForm.WidthValue;

        tempFmTable.IBQuery1.SQL.Clear;
        tempFmtable.IBQuery1.SQL.Add('select * from ' + table.Name);
        tempFmtable.IBQuery1.Open;
        tempFmtable.DragKind:=dkDock;
        tempFmtable.DragMode:=dmAutomatic;

        i := 0;
        querystr:='select ';
        while i <= tForm.ComponentCount - 1 do
          begin
          if tForm.Components[i] is TableFieldInfo then
          begin
            //querystr:='select ';
            //field := nil;
            //tempFmtable.Show;
            field := tForm.Components[i] as TableFieldInfo;
            tempFmtable.Caption:=tForm.Caption;
            tempFmtable.DBGrid1.Columns[i].FieldName := field.FieldNameValue;
            tempFmtable.DBGrid1.Columns[i].Title.Caption := field.DescriptionValue;
            tempFmtable.DBGrid1.Columns[i].Index := field.IndexValue;
            tempFmtable.DBGrid1.Columns[i].Visible := field.VisibleValue;
            tempFmtable.DBGrid1.Columns[i].Width:=field.WidthValue;
            //tempFmtable.CheckListBox1.Items.Add(field.Description);
            //tempFmtable.CheckListBox1.Checked[i] := field.Visible;
            //tempFmtable.Show;
             if field.VisibleValue  then querystr:=querystr+ConvertNameField(field.FieldNameValue)+', ';
          end;
            //inc(i);
          if tForm.Components[i] is SettingsFormInfo then
            begin
              //sForm:=nil;
              sForm:=tForm.Components[i] as SettingsFormInfo;
              tempsettings := TfmSettings.Create(tempFmtable);
              tempsettings.Caption:=sForm.Caption;
              //tempsettings := TfmSettings.Create(tempFmtable);
              tempsettings.Name := sForm.Name;
              tempsettings.Left := sForm.LeftValue;
              tempsettings.Top := sForm.TopValue;
              tempsettings.Height := sForm.HeightValue;
              tempsettings.Width := sForm.WidthValue;
              tempSettings.CheckListBox1.Clear;
              //for k:=0 to sForm.ComponentCount-1 do
              k:=0;
              while k<=sForm.ComponentCount-1 do
                begin
                  if sForm.Components[k] is ItemCheckListBoxInfo then
                    begin
                      //item:=nil;
                      item:=sForm.Components[k] as ItemCheckListBoxInfo;
                      tempsettings.CheckListBox1.Items.Add(item.TextValue);
                      tempsettings.CheckListBox1.Checked[item.IndexValue]:=item.CheckedValue
                    end;
                  if sForm.Components[k] is LabelInfo then
                    begin
                      //labelobj:=nil;
                      labelobj:=sForm.Components[k] as LabelInfo;
                      tempsettings.Label1.Caption:=labelobj.CaptionValue;
                    end;
                  if sForm.Components[k] is EditInfo then
                    begin
                      //edit:=nil;
                      edit:=sForm.Components[k] as EditInfo;
                      TEdit(tempsettings.FindComponent(edit.Name)).Text:=edit.TextValue;
                    end;
                    inc(k);
                end;
             if ((tForm.Components[i] is SettingsFormInfo) and (i=tForm.ComponentCount-1) and (k=sForm.ComponentCount)) then tempsettings.Show;
            end;
             //inc(i);
             //if ((tForm.Components[i] is SettingsFormInfo) and (i=tForm.ComponentCount-1) and (k=sForm.ComponentCount)) then tempsettings.Show;
             inc(i);
          end;

        {for i := 0 to table.Fields.ComponentCount - 1 do
        begin
          descr := Utf8ToAnsi(((table.Fields.Components[i]) as TFieldSpec).Description);
          tempFmtable.CheckListBox1.Items.Add(descr);
          tempFmtable.DBGrid1.Columns[i].Title.Caption := descr;
          tempFmtable.CheckListBox1.Checked[i] := true;
        end; }

        Delete(querystr, Length(querystr)-1, 1);
        querystr:=querystr+'from '+table.Name;
        tempFmTable.IBQuery1.SQL.Clear;
        tempFmtable.IBQuery1.SQL.Add(querystr);
        tempFmtable.IBQuery1.Open;
        for s := 0 to tForm.ComponentCount-1 do
          begin
            if tForm.Components[s] is TableFieldInfo then
              begin
                //field := nil;
                //tempFmtable.Show;
                field := tForm.Components[s] as TableFieldInfo;
                if field.VisibleValue then
                   begin
                     for t := 0 to tempFmtable.DBGrid1.Columns.Count-1 do
                       begin
                       if ((tempFmTable.DBGrid1.Columns[t].Title.Caption=field.DescriptionValue) and (tempFmtable.DBGrid1.Columns[t].FieldName=field.FieldNameValue)) then
                           tempFmTable.DBGrid1.Columns[t].Width:=field.WidthValue;
                       end;
                   end;
              end;
          end;
        tempFmtable.Show;
它在前一行中初始化

tForm := mForm.Components[j] as TableFormInfo;
但尽管在装配过程中出现了此警告

[dcc32 Warning] SerAndDeser.pas(298): W1036 Variable 'tForm' might not have been initialized

为什么??如何在不丢失应用程序功能的情况下消除警告?

您的缩进完全错误。我想这就是让你困惑的地方。如果代码缩进正确,则如下所示:

for j := 0 to mForm.ComponentCount - 1 do
  if mForm.Components[j] is TableFormInfo then
    // try
    // table := nil;
    // tempFmtable := nil;
    // tForm := nil;

    tForm := mForm.Components[j] as TableFormInfo;

table := TTableSpec(DBSchema.Tables.FindComponent(tForm.table));
我认为现在可以清楚地看到,for循环后面的代码可以在没有指定tForm的情况下执行

从问题中的缩进可以清楚地看出,您希望赋值后的长代码段位于if语句中。但是编译器遵循代码而不是缩进

您需要向代码中添加一些开始/结束对。就我个人而言,我从不使用单语句语法——我所有的块都是用begin/end对包装的复合块。在我看来,单语句语法是Pascal最大的错误之一。事实上,C

我使用内置的代码格式化程序来理顺缩进。这可能是一个有用的工具,可以帮助您修复代码。它可以让你掌握压痕的正确布局


最后,正如我之前至少对您说过的,不要将tForm用作变量名。这将隐藏名为TForm的类型。T前缀是为类型保留的。您应该调用表单变量form,或者使用L表示它是局部变量的LForm。

您的缩进都是错误的。我想这就是让你困惑的地方。如果代码缩进正确,则如下所示:

for j := 0 to mForm.ComponentCount - 1 do
  if mForm.Components[j] is TableFormInfo then
    // try
    // table := nil;
    // tempFmtable := nil;
    // tForm := nil;

    tForm := mForm.Components[j] as TableFormInfo;

table := TTableSpec(DBSchema.Tables.FindComponent(tForm.table));
我认为现在可以清楚地看到,for循环后面的代码可以在没有指定tForm的情况下执行

从问题中的缩进可以清楚地看出,您希望赋值后的长代码段位于if语句中。但是编译器遵循代码而不是缩进

您需要向代码中添加一些开始/结束对。就我个人而言,我从不使用单语句语法——我所有的块都是用begin/end对包装的复合块。在我看来,单语句语法是Pascal最大的错误之一。事实上,C

我使用内置的代码格式化程序来理顺缩进。这可能是一个有用的工具,可以帮助您修复代码。它可以让你掌握压痕的正确布局


最后,正如我之前至少对您说过的,不要将tForm用作变量名。这将隐藏名为TForm的类型。T前缀是为类型保留的。您应该调用表单变量form,或者使用L表示它是局部变量的LForm。

IOW如果mForm.ComponentCount为零,则不会在表赋值中赋值该tForm。@jachgrate或者如果不满足if语句。但问题远比这严重得多。编码人员显然打算在if!中包含多个语句!也许你是对的,看看这个注释和缩进。不幸的是,代码是这样的,我们不知道这是否有效,因为新开发人员似乎只是想摆脱这些警告。@jachgrate如果你仔细阅读问题,你会发现编码人员的期望和我说的一样。我的意思是,它是在前一行初始化的。如果可以,我会再次投票支持关于单语句syntax.IOW的段落。如果mForm.ComponentCount为零,则在表赋值中不会赋值该tForm。@jachgrate或如果不满足if语句。但问题远比这严重得多。编码人员显然打算在if!中包含多个语句!也许你是对的,看看这个注释和缩进。不幸的是,代码是这样的,我们不知道这是否有效,因为新开发人员似乎只是想摆脱这些警告。@jachgrate如果你仔细阅读问题,你会发现编码人员的期望和我说的一样。我的意思是,它是在前一行初始化的。如果可以的话,我会再次投票支持关于单语句语法的段落。