Solved

Delphi Copy funtion

Posted on 2014-10-02
12
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 54

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 54

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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 54

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 54

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 54

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 54

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 54

Expert Comment

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

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

732 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