Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function eliminated by linker

Status
Not open for further replies.

carwashguy

Programmer
Feb 22, 2005
3
ZA
Hi,

I'm trying to read from a comma-delimited textfile into a stringlist, using the CommaText property. However when the code is executed I observe the following error:

"Function to be called, TStrings.GetCommaText, was eliminated by linker"

My code is as follows:

************************************************
function TfrmMain.ReadTextFile : boolean;
var
fDataFile: TextFile;
sLineJustRead: string;
aDataList : TStringList;
i : Integer;

begin
aDataList := TStringList.Create;
AssignFile(fDataFile, txtDataFile.Text);
Reset(fDataFile);

While Not SeekEof(fDataFile) Do
begin

Readln(fDataFile, sLineJustRead);
aDataList.CommaText := sLineJustRead;

end;

CloseFile(fDataFile);

end;
************************************************

Any help would be greatly appreciated as this is quite urgent.

Thanks
 
Why not use the LoadFromFile method of the string list?

aDataList.LoadFromFile(txtDataFile.Text);

This way you don't have to open/loop through/close the file in code because the TStringList will handle all that for you.

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
Is this an error or a warning?

I dont see any reference to getcommatext in your code, and there dosn't seem to be a getcommatext method of TStrings (I have a fairly old version of Delphi here D3)
If your version of TStrings has been modified to include this method and its not being called anywhere, then the linker maight remove the code, but this would be a warning (or hint situation) and it would still run.

What happens if you do an F1 over GetCommaText?





Steve
Time for a new sig a bit of DNA I think will scan books and get back to you. It's taking a while isn't it!
 
Steve

From Delphi 5 onwards (not sure about D4) there is a GetCommaText function in TStrings, and as you would expect it is the 'read' function for the CommaText property:

Code:
    property CommaText: string read GetCommaText write SetCommaText;

However, in the code snippet from carwashguy CommaText is only being written to, not read from. It is therefore reasonable to assume that the linker may have included SetCommaText but eliminated GetCommaText. If carwashguy is then trying to view the contents of CommaText (e.g. with a watch or using evaluate/modify) then the error message would be shown.
 
Oh...Something else I just saw in the code.... When you set the CommaText to the line that was just read in, you're overwriting what was already there. So, from your code, the stringlist will always have ONLY the last line that was read from the file. If you're running into this while debugging and trying to figure out why you're not getting the results you expect, that's why.

If you have to load the file line by line (instead of using LoadFromFile) you'll want to change the line where you're setting CommaText to instead read:
Code:
aDataList.Add(sLineJustRead);

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
As this is a function, shouldnt the line:
Code:
AssignFile(fDataFile, txtDataFile.Text);

be:
Code:
AssignFile(fDataFile, frmMain.txtDataFile.Text);

I know my function can't read from functions unless I either make my function like above or like this

Code:
function SomeFuncyStuff(MyEdit: TEdit): Boolean;
begin
... bla bla bla ...
end;

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
... sigh, I suck ...

word "functions" should be objects on the last line.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
BobbaFet, There are at least three ways to set up functions in a Delphi forms unit:

1. Global
2. Local
3. Member of the form. Also called a method of the form which can be either public or private.

When you posted that you need to call your function this way:
[tt]
if SomeFuncyStuff(frmMain.Edit1) then
begin
:
:
end;
[/tt]
and your function is written like this:
[tt]
function SomeFuncyStuff(MyEdit: TEdit): Boolean;
begin
... bla bla bla ...
end;
[/tt]
that suggests to me that you have used the second way in my list (Local). In other words, you have the function implemented in the implementation section and do not have a prototype declared anywhere in the interface section.

If you look closely at carwashguy's function definition you will see the reference to TfrmMain:
[tt]
function TfrmMain.ReadTextFile : boolean;
var
fDataFile: TextFile;
:
:
begin
:
AssignFile(fDataFile, txtDataFile.Text);
:
:
CloseFile(fDataFile);
end;
[/tt]
In this case, "ReadTextFile" is a method of TfrmMain and has complete access to all of TfrmMain's members, in particular it can reference txtDataFile without further qualification. This was accomplished by declaring the function prototype in the interface section of the unit as a public or private method of TfrmMain:
[tt]
type
TfrmMain = class(TForm)
:
:
private
:
function ReadTextFile : boolean;
:
public
:
:
end;
[/tt]
Your function works because the parameter is a TEdit and as long as you call it with a TEdit, the compiler is happy. The function doesn't know where the TEdit came from (although it can look at the parent property to find out) it is the caller that must either have the TEdit in scope or otherwise must qualify it with the parent name (e.g. frmMain.Edit1).

To make the function global, simply put the declaration on its own line in the implementation section, outside of any class definition.

Hope this helps.
 
Oh, I didn't know that was possible with functions! I thought that was restricted to procedures only, but thanks Zarthras, now I know!

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
There are also "Class Functions" and "Class Procedures" that can be called without having to instantiate the object first.

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
Hi all,

Thanks for all your help but I actually discovered the code seems to be working despite the "errors" that I'm getting. I say "errors" because the error message that I mentioned in my first post only appears if I hover my mouse cursor over 'aDataList.CommaText'. What I do after this point in the code is iterate through the stringlist and insert each value into the fields of a database table, and this seems to be working fine. Still would be interested to know if anyone can tell me why I'm getting this linker error.

Thanks again
 
carwashguy, are you using Delphi 2005?

--------------------------------------
What You See Is What You Get
 
seems to me a code insight related bug. did you install the latest update for delphi7??

--------------------------------------
What You See Is What You Get
 
As I said earlier, I believe this will be because carwashguy's code only sets CommaText, it does not get CommaText. The linker has therefore removed the GetCommaText function because it is not needed by the program. When carwashguy hovers over CommaText, code insight will obviously try to call GetCommaText to get the value, and hence he gets the error reported.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top