bravesofts 6 Posted September 22, 2021 (edited) 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 September 22, 2021 by bravesofts Share this post Link to post
David Heffernan 2345 Posted September 22, 2021 I don't understand the question. Obviously the code here is not valid. Can you describe what you want to achieve. Share this post Link to post
bravesofts 6 Posted September 22, 2021 (edited) 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 September 22, 2021 by bravesofts Share this post Link to post
Lajos Juhász 293 Posted September 22, 2021 type TTwenty: string [20]; This is a shortstring and should not be used in programs as it is only for backward compatibility. 1 Share this post Link to post
Vandrovnik 214 Posted September 22, 2021 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...). 1 Share this post Link to post
David Heffernan 2345 Posted September 22, 2021 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. 1 Share this post Link to post
bravesofts 6 Posted September 22, 2021 (edited) 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 September 22, 2021 by bravesofts Share this post Link to post
Guest Posted September 22, 2021 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 Share this post Link to post
Fr0sT.Brutal 900 Posted September 23, 2021 ShortString is limited to 255 characters so you have not so much types to declare Share this post Link to post