Forum Home
Press F1
 
Thread ID: 47686 2004-08-02 09:50:00 Delphi 7 problem - function not working Bletch (244) Press F1
Post ID Timestamp Content User
257669 2004-08-02 09:50:00 Hi

I have been writing a program in delphi 7, and am having problems getting one of my functions to work. The application goes fine until I add the line

if Auth(Anly[1]) = true then begin

to the code. At this point, the program begins to do strange, irrelevant things, even when the function in question is totally empty, or is set to always equal true (which should cause the app to keep running as though nothing happened). The function is called 'Auth', and is properly declared. It takes a string as the input parameter and returns a boolean. It is the only statement like this in my whole app, so I don't know if this would be caused by any function. I could really use some help with this, as I have been bashing my head against a wall of code all afternoon and have got nowhere.

Bletch

NOTE: The app also uses the Internet Direct (INDY) networking components 'IdTCPServer' and 'IdTCPClient' to transfer text, but only text among windows 2000 and XP machines. It then performs various analyses on this text.
Bletch (244)
257670 2004-08-02 10:48:00 >if Auth(Anly[1]) = true then begin
>It takes a string as the input parameter and returns a boolean
Ok so you are using the first character of the string to return true or false.
Well unless the first character is always a 0 or 1 you will get bizare results because boolean uses 0 or 1 to determin true or false.
You need to put it something like this
>> if Auth(Anly[1]) = whatever then boolean var := whatever;
mikebartnz (21)
257671 2004-08-02 10:50:00 > if Auth(Anly[1]) = whatever then boolean var := whatever;
Should be
>> if Auth(Anly[1]) = whatever then boolean var := true or false;
mikebartnz (21)
257672 2004-08-03 05:21:00 If you are trying to cheat by using a non-zero length string to mean true, and an empty string to cause the funtion to return false, you should use the length byte ...
blah[0]. :-) But that would be bad Pascal. X-(

But if you are actually using the first character, what about showing us the essential lines of the function?

There's nothing wrong with that if statement. So there's something wrong in the function.
Graham L (2)
257673 2004-08-03 05:42:00 Thanks for the answers ppl, BUT: It takes a string as a parameter, not the first character of a string. The 'Anly[1]' refers to a line in a string list declared as a local variable in the procedure that calls it. The function then uses that data (usually an IP address or NetBios hostname) to connect to another computer on the LAN and do its thing. The boolean result that the function returns is an indication of whether to continue executing the rest of the calling procedure based on the eveluation of the returned string. (To keep the program running correctly). The variable 'GlobalPassword' is a global variable.

Note: This is by no means a finished function, and most of the error-correction has been taken out to keep it brief.

function TFormServer.Auth(Host : string) : boolean;
var
Password : string;
begin
if NeedPass then begin

if Client.Connected then
Client.Disconnect;
Client.Host := Host;
Client.Connect(-1);
Client.WriteLn('$pass?$end');
Password := Client.ReadLn('$end',-1);
except
AddMemo('ERROR: Failure asking '+Host+' for a password');
if Client.Connected then
Client.Disconnect;
end;

try
if Password = GlobalPassword then
result := true
else
result := false;
finally
if Client.Connected then
Client.Disconnect;
end;
end else
result := true;
end;

Bletch
Bletch (244)
257674 2004-08-03 05:46:00 sorry about no indentation, but the posting form seems to strip out unneccessary spaces. This function compiles properly as it is, but when I add the line calling it the whole app seems to become unpredictable and do random things I don't understand. Bletch (244)
257675 2004-08-03 06:08:00 Yes, I was muttering things like "hasn't he heard of indented listings?". :D

In future, the [ pre ] [ / pre ] brackets do the job.

My favourite argument for Pascal is that "when it compiles it usually works" as opposed to C, Fortran, etc.

I'm wondering about the ends ... I've carefully avoided this sort of OO code, so I'm assuming by necessity from ignorance that the "try, finally, except" tokens are like "begin" and must be matched with ends. In that case, the first block might be missing an end? This could make the logic of the rest a bit less deterministic. ;-)

In writing many thousand lines of Pascal I learned early to comment just about every end to avoid confusion (mine). It stops me from "adding ends until it compiles". :D

thus:program fred(input, output);
function joe(var inp):boolean;
begin
...
...
end { joe };
begin
if joe(1) then begin
... ;
... ;
end { if joe };
...
...
end { program }.
Graham L (2)
257676 2004-08-03 09:09:00 I found the problem!!! It turned out to be totally unrelated, something to do with the Delphi environment or windows 2000. It went away when I rebooted windows, instead of hibernating it with Delphi open. (Although I still have no idea why the problem even occurred in the first place). Thanks ppl.

Bletch
Bletch (244)
1