Link to home
Start Free TrialLog in
Avatar of Rodbach
RodbachFlag for Afghanistan

asked on

Delphi Copy funtion

I'm using Delphi 5 and I ran into a problem and cannot find the answer.
I'm trying to split a string into two parts. The string is 'AWT 56004' and this is the code.

  NumbChars := Copy('AWT 65231', 5, 9);
  AlphaChars := Copy('AWT 56004', 1, 4);

The first line produces '56004' as expected.
After the second line is processed both variables are a blank string.

Originally the parameters were variables but in an effort to find the answer I resorted to using constants. Same result.
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

I presume you meant to say either that the first line is

NumbChars := Copy('AWT 56004', 5, 9);

or that the first line produces '65231' as expected.

In any case, my first thought is that AlphaChars (and maybe NumbChars) is not defined as a string. Regards, Joe
Avatar of Rodbach

ASKER

NumbChars and AlphaChars are local variables. And yes I misquoted I should have said ''65231' as expected.

Funny thing is that even if I changed the second line to a straight assignment, i.e., AlphaChars  := 'AWT'; immediately after executing the second line both vars become a blank string.
> immediately after executing the second line both vars become a blank string

Since you say that they are both local variables, the only way I can see that happening is if the code which is showing you that both vars are empty strings is accessing variables with those same names, but that are in a different scope. Where is the code with the assignment statements? Where is the code that is displaying the values after the assignment statements have run? Regards, Joe
ASKER CERTIFIED SOLUTION
Avatar of Rodbach
Rodbach
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just noticed the ":+" operator in your AlphaChars assignment statements. Do you really want that? Your NumbChars assignment statements have the ":=" operator.

I don't use Delphi — I do my Pascal programming in Free Pascal, which does have a Delphi compatibility mode, although it is not 100%. I don't know if ":+" is a valid operator in Delphi, but it is not valid in Free Pascal — neither in FP Dialect nor Delphi Compatible compiler modes. Both modes throw a compile error.

I fed the following program to FP:
program EE_Delphi_Problem;
var
  AlphaChars: string;
  NumbChars: string;
begin
  writeln('before assignment: AC=',AlphaChars,' NC=',NumbChars);
  NumbChars := Copy('AWT 65231', 5, 9);
  AlphaChars := Copy('AWT 56004', 1, 4);
  writeln('after assignment: AC=',AlphaChars,' NC=',NumbChars);
end.

Open in new window

I compiled it in both FP Dialect and Delphi Compatibility compiler modes. In both cases, the executable produces the correct output, as follows:

before assignment: AC= NC=
after assignment: AC=AWT  NC=65231

Regards, Joe
Avatar of Rodbach

ASKER

Mistype on my behalf. The compiler did catch it. Should have been a '=', but my little finger hit the Shift key at the wrong moment! :-(

I haven't tried it in Free Pascal, but it definitely behaves the way I describe it in Delphi 5, Funnily enough in XE7 it works correctly. Unfortunately this client is still in the Delphi 5 era!!!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rodbach

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for Rodbach's comment #a40360287

for the following reason:

Because I solved teh problem myself with a workaround.
I was happy to help, but don't you think the help I gave you in testing and confirming that it's a Delphi 5 bug is worth any credit? You could certainly mark your own workaround as the Accepted Solution, but don't you think my posts are worth some points as Assisted Solutions?

Note that your original question was:
I'm using Delphi 5 and I ran into a problem and cannot find the answer.
The answer is that it's a Delphi 5 bug. Regards, Joe
Hi EE Admin,
I haven't heard back from the asker in two days and now there are only two days left in the 4-day close period, so I'm submitting this objection. I believe that I deserve some credit for showing that the answer is that it is a Delphi 5 bug ("I'm using Delphi 5 and I ran into a problem and cannot find the answer."). Please take a look at the thread and render what you think is a fair judgment. I will accept whatever decision you make without any further objection. Thank you for your time. Regards, Joe
Hi eenookami,
Thank you for the prompt response — much appreciated! Regards, Joe