Solved

Newbie: variable 'f' might not have been initialize.

Posted on 2004-04-23
4
255 Views
Last Modified: 2010-04-05
Purpose of this procedure is to display the child form if one already exist. I dont know how to remove the warning. Newbie to delphi. how do i fix this?

procedure TMainForm.CreateMDIChild(frm: TFormClass);
var
  i:integer;
  bCreated:boolean;
  f:TForm;
begin
    bCreated:=False;
    for i:=0 to MainForm.MDIChildCount-1 do
    begin
      if MainForm.MDIChildren[i] is frm then
      begin
        f:=MainForm.MDIChildren[i] as frm;
        bCreated:=True;
        Break;
      end;
    end;

    if not bCreated then
    begin
      frm.Create(Application);
    end
    else
    begin
        f.Show; //<-----<< warning here
    end;
end;
0
Comment
Question by:Jerry_Pang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10906550
Can't You do like this?

procedure TMainForm.CreateMDIChild(frm: TFormClass);
var
  i:integer;
  bCreated:boolean;
  f:TForm;
begin
    bCreated:=False;
    for i:=0 to MainForm.MDIChildCount-1 do
    begin
      if MainForm.MDIChildren[i] is frm then
      begin
        f:=MainForm.MDIChildren[i] as frm;
        bCreated:=True;
        f.Show;
        Break;
      end;
    end;

    if not bCreated then
    begin
      frm.Create(Application);
    end
end;

0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 50 total points
ID: 10907195

   You seen warning because    
   f:=MainForm.MDIChildren[i] as frm;
   is in IF statement and some times the condition can be FALSE. You can about this if you type

 begin
    F := nil;
    ....
    // YOUR CODE
 
0
 
LVL 9

Author Comment

by:Jerry_Pang
ID: 10914728
mokule,
i cant. because that will always create a an instance of a form.
if not bCreated then
 Create the form.
else
 show the form.
endif

0
 
LVL 17

Expert Comment

by:mokule
ID: 10915712

You probably did not notice that the same is in my code :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question