Solved

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

Posted on 2004-04-23
4
238 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now