/ Forside / Teknologi / Udvikling / Delphi/Pascal / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
Delphi/Pascal
#NavnPoint
oldwiking 603
jrossing 525
rpje 520
EXTERMINA.. 500
gandalf 460
gubi 270
DJ_Puden 250
PARKENSS 230
technet 210
10  jdjespers.. 200
Q:Why: Variable 'TempM' might not have bee~
Fra : No Junk Mail


Dato : 04-04-01 13:25

Dear Groups

I'm trying to declare a local variable of type TMemo (or TStrings) in a
function, but it keeps giving me:

[Warning] change.pas(89): Variable 'TempM' might not have been
initialized

Is it illigal to declare variables of type TMemo when they don't appear
on a form?
How do I init a variable like that? .Clear gives runtime error...

Please...

The code looks like this:

function FindDiffByLine(newer:TMemo;older:TMemo):TMemo;
var
i: integer;
TempM: TMemo;
begin
if not newer.Lines.Equals(older.Lines) then
begin
//Find new lines
for i:=0 to older.Lines.count-1 do
begin
if newer.Lines.IndexOf(older.Lines.Strings[i])=-1 then
TempM.Lines.append('IN: '+older.Lines.Strings[i]);
end;
end; //if not newer.Equals(older)
result := TempM;
end;

Best Regards
Martin Hvidberg
mhv@fsl.dk.nospam


 
 
M.H. Avegaart (04-04-2001)
Kommentar
Fra : M.H. Avegaart


Dato : 04-04-01 13:36

TMemo is a class. Before you can use a variable of a class type you have to
create an instance, e.g.:

function FindDiffByLine(Newer, Older: TMemo): TMemo;
var
i: integer;
begin
Result := TMemo.Create(nil);
if not Newer.Lines.Equals(Older.Lines) then
begin
for i := 0 to Older.Lines.Count - 1 do
if Newer.Lines.IndexOf(Older.Lines[i]) < 0 then
Result.Lines.Add('IN: ' + Older.Lines[i]);
end;
end;

ps. Don't forget to Free the TMemo returned from the function ! Normally you
would supply the result memo as a third parameter and use it in the function
(or in that case procedure).


"No Junk Mail" <NO@Junk.Mail> schreef in bericht
news:3ACB1283.A1FC519E@Junk.Mail...
> Dear Groups
>
> I'm trying to declare a local variable of type TMemo (or TStrings) in a
> function, but it keeps giving me:
>
> [Warning] change.pas(89): Variable 'TempM' might not have been
> initialized
>
> Is it illigal to declare variables of type TMemo when they don't appear
> on a form?
> How do I init a variable like that? .Clear gives runtime error...
>
> Please...
>
> The code looks like this:
>
> function FindDiffByLine(newer:TMemo;older:TMemo):TMemo;
> var
> i: integer;
> TempM: TMemo;
> begin
> if not newer.Lines.Equals(older.Lines) then
> begin
> //Find new lines
> for i:=0 to older.Lines.count-1 do
> begin
> if newer.Lines.IndexOf(older.Lines.Strings[i])=-1 then
> TempM.Lines.append('IN: '+older.Lines.Strings[i]);
> end;
> end; //if not newer.Equals(older)
> result := TempM;
> end;
>
> Best Regards
> Martin Hvidberg
> mhv@fsl.dk.nospam
>



Carlo Kok (04-04-2001)
Kommentar
Fra : Carlo Kok


Dato : 04-04-01 13:41


"No Junk Mail" <NO@Junk.Mail> wrote in message
news:3ACB1283.A1FC519E@Junk.Mail...
> Dear Groups
>
> I'm trying to declare a local variable of type TMemo (or TStrings) in a

TMemo <> TStrings.

TMemo is the control that allows to edit TStrings collection.
TStrings should not be created because it contains abstract methods. Use
TStringList instead.

In your question I suggest you use TStrings/TStringList.

> function, but it keeps giving me:
>
> [Warning] change.pas(89): Variable 'TempM' might not have been
> initialized
>
> Is it illigal to declare variables of type TMemo when they don't appear
> on a form?
> How do I init a variable like that? .Clear gives runtime error...
>
> Please...
>
> The code looks like this:
>
> function FindDiffByLine(newer:TMemo;older:TMemo):TMemo;
> var
> i: integer;
> TempM: TMemo;
> begin
> if not newer.Lines.Equals(older.Lines) then
> begin
> //Find new lines
> for i:=0 to older.Lines.count-1 do
> begin
> if newer.Lines.IndexOf(older.Lines.Strings[i])=-1 then
> TempM.Lines.append('IN: '+older.Lines.Strings[i]);

You have not created tempM before using it.

> end;
> end; //if not newer.Equals(older)
> result := TempM;
> end;
>


function FindDiffByLine(newer, Older: TStrings):TStrings;
var
i: integer;
begin
Result := TStringList.Create;
if not newer.Equals(older) then
begin
//Find new lines
for i:=0 to older.count-1 do
begin
if newer.IndexOf(older[i])=-1 then
Result.Append('IN: '+older[i]);
end;
end;
end;




Dennis Edward (04-04-2001)
Kommentar
Fra : Dennis Edward


Dato : 04-04-01 23:49

You are getting this message because, not surprisingly, TempM hasn't been
initialized.

When you declare an object in the "var" section, it isn't initialized. You
still have to say "TempM:=TMemo.Create" to create an actual instance of the
object. In the conditional in the middle of your loop, you are using an
uninitialized variable: TempM.


"No Junk Mail" <NO@Junk.Mail> wrote in message
news:3ACB1283.A1FC519E@Junk.Mail...
> Dear Groups
>
> I'm trying to declare a local variable of type TMemo (or TStrings) in a
> function, but it keeps giving me:
>
> [Warning] change.pas(89): Variable 'TempM' might not have been
> initialized
>
> Is it illigal to declare variables of type TMemo when they don't appear
> on a form?
> How do I init a variable like that? .Clear gives runtime error...
>
> Please...
>
> The code looks like this:
>
> function FindDiffByLine(newer:TMemo;older:TMemo):TMemo;
> var
> i: integer;
> TempM: TMemo;
> begin
> if not newer.Lines.Equals(older.Lines) then
> begin
> file://Find new lines
> for i:=0 to older.Lines.count-1 do
> begin
> if newer.Lines.IndexOf(older.Lines.Strings[i])=-1 then
> TempM.Lines.append('IN: '+older.Lines.Strings[i]);
> end;
> end; file://if not newer.Equals(older)
> result := TempM;
> end;
>
> Best Regards
> Martin Hvidberg
> mhv@fsl.dk.nospam
>



Jim Korman (05-04-2001)
Kommentar
Fra : Jim Korman


Dato : 05-04-01 03:29

No Junk Mail wrote:
>
> Dear Groups
>
> I'm trying to declare a local variable of type TMemo (or TStrings) in a
> function, but it keeps giving me:
>
> [Warning] change.pas(89): Variable 'TempM' might not have been
> initialized
>
> Is it illigal to declare variables of type TMemo when they don't appear
> on a form?
> How do I init a variable like that? .Clear gives runtime error...
>
> Please...
>
> The code looks like this:
>
> function FindDiffByLine(newer:TMemo;older:TMemo):TMemo;
> var
> i: integer;
> TempM: TMemo;
> begin
> if not newer.Lines.Equals(older.Lines) then
> begin
> //Find new lines
> for i:=0 to older.Lines.count-1 do
> begin
> if newer.Lines.IndexOf(older.Lines.Strings[i])=-1 then
> TempM.Lines.append('IN: '+older.Lines.Strings[i]);
^^^^^
When you reference TempM here you haven't created a new TMemo yet.
That is what the compiler is complaining about!

> end;
> end; //if not newer.Equals(older)
> result := TempM;
> end;
>
> Best Regards
> Martin Hvidberg
> mhv@fsl.dk.nospam

Thomas P (05-04-2001)
Kommentar
Fra : Thomas P


Dato : 05-04-01 14:29

Hej alle andre i gruppen... læg lige mærke til at dette indlæg fra "No Junk
Mail" simpelthen er X-postet på kryds og tværs. Personligt synes jeg det er
lidt grimt, men prøv at kigge lidt på usenets retningslinier

- Thomas



Niels (05-04-2001)
Kommentar
Fra : Niels


Dato : 05-04-01 16:46

Just as I expected, Thomas P came up with this:

>Hej alle andre i gruppen... læg lige mærke til at dette indlæg fra "No Junk
>Mail" simpelthen er X-postet på kryds og tværs. Personligt synes jeg det er
>lidt grimt, men prøv at kigge lidt på usenets retningslinier

Ja, jeg har vist set det i en 4-5 grupper nu!

Niels
--
e-mail: nmartin at iname dot com
ICQ UIN: 50187323
http://www.niller.f2s.com/ - niLLer's pages

Osmo Ronkanen (18-04-2001)
Kommentar
Fra : Osmo Ronkanen


Dato : 18-04-01 00:20

In article <3ACB1283.A1FC519E@junk.mail>, No Junk Mail <Mhv@fsl.dk> wrote:
>Dear Groups
>
>I'm trying to declare a local variable of type TMemo (or TStrings) in a
>function, but it keeps giving me:
>
>[Warning] change.pas(89): Variable 'TempM' might not have been
>initialized

You initialize it only behind a condition.

>
>Is it illigal to declare variables of type TMemo when they don't appear
>on a form?

????

>How do I init a variable like that? .Clear gives runtime error...

You initialize a variable by setting a value to it.

>
>Please...


>
>The code looks like this:
>
>function FindDiffByLine(newer:TMemo;older:TMemo):TMemo;
>var
> i: integer;
> TempM: TMemo;
>begin
> if not newer.Lines.Equals(older.Lines) then
> begin
> //Find new lines
> for i:=0 to older.Lines.count-1 do
> begin
> if newer.Lines.IndexOf(older.Lines.Strings[i])=-1 then
> TempM.Lines.append('IN: '+older.Lines.Strings[i]);

Is this always executed? If not, then there is an error. The compiler
gives you a warning because it might go uninitialized. I think there is
some bug in the code.

> end;
> end; //if not newer.Equals(older)
> result := TempM;
>end;
>
>Best Regards
>Martin Hvidberg
>mhv@fsl.dk.nospam
>

Osmo

Søg
Reklame
Statistik
Spørgsmål : 177558
Tips : 31968
Nyheder : 719565
Indlæg : 6408927
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste