Solved

Delphi Copy funtion

Posted on 2014-10-02
12
206 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 52

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 52

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 52

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 52

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 52

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 52

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 52

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to use the Hootsuite Dashboard.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

920 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

15 Experts available now in Live!

Get 1:1 Help Now