Solved

Delphi Copy funtion

Posted on 2014-10-02
12
212 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 53

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 53

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 53

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
 

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 53

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 53

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 53

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 53

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Automatic dictation/transcribing software 9 54
ejb entity bean example 2 31
add projects t working set in maven 2 37
Pay to 支付宝 12 64
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.

680 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