Jump to content
Ian Branch

IsNullOrWhiteSpace???

Recommended Posts

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 by Ian Branch

Share this post


Link to post

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

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

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 by programmerdelphi2k
  • Like 1

Share this post


Link to post

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

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
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

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 by Fr0sT.Brutal

Share this post


Link to post

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

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

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

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

×