Ian Branch 127 Posted March 25, 2023 (edited) Hi Team, I am trying to figure out the wierd usage of the TStringHelper IsNullOrWhiteSpace. I have seen this as "if ''.IsNullOrWhiteSpace(MyString) then", and this "MyString.IsNullOrWhiteSpace(MyString) then". Are both correct? Why the confusing construct? From the class definition, "class function IsNullOrWhiteSpace(const Value: string): Boolean; static;" I had expected to be able to simply do a more readable "if IsNullOrWhiteSpace(MyString) then ". Regards & TIA, Ian Edited March 25, 2023 by Ian Branch Share this post Link to post
Attila Kovacs 629 Posted March 25, 2023 Use it as TStringHelper.IsNullOrWhiteSpace() in order to avoid misunderstandings. Your examples are just the side effects of this record helper on the string type. Share this post Link to post
Ian Branch 127 Posted March 25, 2023 Hi Attila, I tried that with this "if TStringHelper.IsNullOrWhiteSpace('sMyString') then" but get an 'E2018 Record, object or class type required' error. Share this post Link to post
Ian Branch 127 Posted March 25, 2023 I've just settled for "if Trim(MyString) = '' then" Share this post Link to post
programmerdelphi2k 237 Posted March 26, 2023 (edited) if you see in "source code", you should can ignored this, in better choice! Quote class function TStringHelper.IsNullOrWhiteSpace(const Value: string): Boolean; begin Result := Value.Trim.Length = 0; end; better would be, directly "if LVar.Trim.Length = 0 then ...", no pain, no gain.... no trap! Edited March 26, 2023 by programmerdelphi2k 1 Share this post Link to post
David Heffernan 2345 Posted March 26, 2023 Surely it's If string.IsNullOrWhiteSpace(someStr) then like in .net Share this post Link to post
david berneda 19 Posted March 26, 2023 I wish a compiler option to warn when calling class methods on instances instead of using the class type. Another one, hint when methods can be safely converted to class or class static. Share this post Link to post
David Schwartz 426 Posted March 26, 2023 You probably don't need the class name in front. and isn't this form also acceptable: myString.IsNullOrWhiteSpace Share this post Link to post
David Heffernan 2345 Posted March 26, 2023 1 hour ago, David Schwartz said: You probably don't need the class name in front. What is the subject of the method then? 1 hour ago, David Schwartz said: and isn't this form also acceptable: myString.IsNullOrWhiteSpace What argument are you going to pass, and what purpose does myString serve? Share this post Link to post
Fr0sT.Brutal 900 Posted March 27, 2023 (edited) I'd too expect it to be instance method. No string in Delphi could be null in fact so there's no sense in static class method. Probably they simple copypasted it from .Net which has nulls and thus such function couldn't be an instance method? Edited March 27, 2023 by Fr0sT.Brutal Share this post Link to post
David Heffernan 2345 Posted March 27, 2023 It doesn't much matter what anybody expects the design to be. Too late for that now. Given that it takes the string as an argument, and it's a static class method, I think it's safe to conclude that you don't call it as an instance method. Wouldn't it just be better if the language forced you to call it on a class? Share this post Link to post
Lars Fosdal 1792 Posted March 27, 2023 It could have had a parameterless overload that could be used for the instance. if string.IsNullOrWhiteSpace(s) then Writeln('Yup'); if s.IsNullOrWhiteSpace then Writeln('Yup'); As it is now, only the first construct is supported. Share this post Link to post
Lars Fosdal 1792 Posted March 27, 2023 There is a lot of strange code in that helper. Have a look at function IsEmpty, which compares the string to a record local const Empty - when the unit already has a global constant EmptyStr. Go figure. Share this post Link to post