Solved

Delphi Copy funtion

Posted on 2014-10-02
12
202 Views
Last Modified: 2014-10-21
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.
0
Comment
Question by:Rodbach
  • 7
  • 4
12 Comments
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40357407
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
0
 

Author Comment

by:Rodbach
ID: 40357659
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.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40358370
> 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
0
 

Accepted Solution

by:
Rodbach earned 0 total points
ID: 40360287
Here is the actual function:

function TfrmAddNewVehicle.IsValidEquipmentId(var aStr: string): Boolean;
var
  k: Integer;
  NumBegin: Integer;
  AlphaChars: string;
  NumbChars: string;
  InStr: string;
begin
  Result := False;
  NumBegin := 0;
  InStr := aStr;
  AlphaChars := '';

  { DONE : Find where the number starts }
  for k: = 1 to Length(InStr) do
  begin
    if InStr[k] in ['0'..'9'] then
    begin
      NumBegin := k;
      Break;
    end;
  end;

  AlphaChars :+ Copy(InStr, 1, NumBegin -1);
  NumbChars := Copy(InStr, NumBegin, Length(InStr));
  NumbChars := StringofChar('0', 10 - Length(NumbChars)) + NumbChars;
  aStr := AlphaChars + NumbChars;
 
  if Length(aStr) <= 14 then
    Result := True;
end;

It is totally contained and the two variables have no scope outside this function. Nor have I got any variables with the same names.

I even tried creating a test procedure as follows

procedure Test(aStr);
  AlphaChars: string;
NumbChars: string;
begin
  AlphaChars :+ Copy(InStr, 1, 4);
  NumbChars := Copy(InStr, 5, 5);
end;

When I stepped through the code I found exactly the same occurrence. AlphaChars was assigned a variable but after executing the second line both variables were set to a blank string.

I solved the problem in the end by building up the AlphaChars string while going through the loop and thus having onnly one line with the Copy function in it.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40361408
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
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Rodbach
ID: 40363726
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!!!
0
 
LVL 51

Assisted Solution

by:Joe Winograd, EE MVE
Joe Winograd, EE MVE earned 500 total points
ID: 40363851
Well, now, that's interesting! So it works in XE7...it works in FP 2.6.4...looks like a Delphi 5 bug to me! I think you made the right call with the work-around of building up the AlphaChars string in the loop. Since that works fine, it bolsters the theory of a Delphi 5 bug. Regards, Joe
0
 

Author Comment

by:Rodbach
ID: 40375784
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.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40373892
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
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40375785
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
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40376161
Hi eenookami,
Thank you for the prompt response — much appreciated! Regards, Joe
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

762 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

18 Experts available now in Live!

Get 1:1 Help Now