Jump to content
bravesofts

delphi set max length for Argument of string in a method

Recommended Posts

is there a possible way to SetLength using [Max Length] for arg of string in a Method ?

function foo(aValue: string [20]): Result;
procedure foo(aValue: string [20]);

this question is just a Curious question and nothing more ..

Edited by bravesofts

Share this post


Link to post
8 minutes ago, David Heffernan said:

I don't understand the question. Obviously the code here is not valid. Can you describe what you want to achieve. 

the valid code that can fit to my question is this:

type
  TTwenty: string [20];

function foo(aValue: TTwenty): Result;
procedure foo(aValue: TTwenty);

i hope my question is clear now

-----------

is ther a way to avoid declaring a new custom types for every arg ?

-----------

Note: My question is Always a Curious question  ..

Edited by bravesofts

Share this post


Link to post
type
  TTwenty: string [20];

 

This is a shortstring and should not be used in programs as it is only for backward compatibility.

  • Like 1

Share this post


Link to post
5 minutes ago, bravesofts said:

the valid code that can fit to my question is this:


type
  TTwenty: string [20];

function foo(aValue: TTwenty): Result;
procedure foo(aValue: TTwenty);

i hope my question is clear now

-----------

is ther a way to avoid declaring a new custom types for every arg ?

string[20] is, AFAIK, ansi string (= expect problems...).

  • Like 1

Share this post


Link to post
11 minutes ago, bravesofts said:

is there a way to avoid declaring a new custom types for every arg?

No there is not.

 

However, you really should not be using short strings at all. These only exist for backwards compatibility with legacy code.

  • Like 1

Share this post


Link to post
11 minutes ago, David Heffernan said:

No there is not.

 

However, you really should not be using short strings at all. These only exist for backwards compatibility with legacy code.

It seems to me that this technique is not implemented yet !! (not only delphi maybe all languages)

---------

and thank you All for the Advice about the shortstring ...

Edited by bravesofts

Share this post


Link to post

If you are on the paranoid side, use asserts or throw an exception.

You could wrap these raises in {$IFDEF DEBUG} compiler directives (or your own XTRADEBUG or some such) if you are hot on performance.

 

HTH

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×