Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String error?

Status
Not open for further replies.

Tilen

Programmer
Apr 2, 2005
75
SI
Hi

is it possible to get an error when trying to put longer string into smaller one?

var
s1:string[5];
s2:string;
begin
s2:='test_string';
s1:=s2; // <- I would like this to show up an error
end;

I have around 1000 string variable which I changed from string -> string[5]. That means there's more than 1000 lines of code where I do s1:=s2; so I don't wanna create my procedure that would check lengths of strings and change all my code.

Is there a simpler way?

Is there a compiler directive that could handle that?

Any suggestion would be appreciated

Thanx a lot
Tilen
 
Not really. The standard action for something like that is truncation.

Besides, if you have 1000 s1 := s2, that's a real big sign you need to have a procedure/function doing the work. Never duplicate anything in the source unless there's a performance benefit to doing so.

If length is a real issue in this case, I'd consider going ahead and writing the length checking code. It'll give you what you need to know, compared to a compiler directive, which will just cause your program to crash.
 
No, I don't have 1000x s2:=s1; but a line or two for each of the 1000 variables. 1000 variables is meant 1000 string variable in 50 different records...

The thing is I only need to check it once, to see if by mistake I put a string[5] for a variable that excepts more than 5 characters.
If the first time is OK, the it will be always.

No problems if it crashes, I just need to find a variable, change it back to string and it will be OK.

 
Use string then it takes the maximum length possible (255 characters)

Steven
 
the string type is no longer limited to 255 characters, but instead to the amount of memory available.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top