Checking for empty strings or lists
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Checking for empty strings or lists

  1. #1
    Join Date
    Jun 2010
    Posts
    56

    Checking for empty strings or lists

    I am doing some validation on a form and I am checking for empty strings or empty lists.

    Is this a proper way to check for an empty string in a text box?

    Code:
    if(Textbox.Text == String.Empty)
    From my understanding this will check to see that the Text field is empty. This also prevents the use of something like the following which does the same thing.

    Code:
    if(Textbox.Text == "")
    Which is basically doing like a magic number where I have a literal string in my code.

    But how would you do this with a List or Listbox? I am trying to prevent the following where I have a hard coded 0.

    Code:
    if(List.Items.Count == 0)

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,830

    Re: Checking for empty strings or lists

    hmm personally I do not have an issue with using "" or 0 in the cases above and would assume that "" may be faster than the string.empty but very little if any at all.

    I do not know if there is a constant for an empty list but if not it would be simple enough to add your own and use it instead of the hard coded 0.
    Always use [code][/code] tags when posting code.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,286

    Re: Checking for empty strings or lists

    For a string, the proper check is to use String.IsNullOrEmpty( str ) method.

    This takes care of the empty string "" and null cases.

    In terms of validation, I would move to one of the model-view-??? design patterns and put the validation in the controller or presenter layer.

    I would also use binding to tie the view to the controller or presenter layer.

    That way, your validation isn't directly in the view, so if you decide to change your view, you won't have to change the validation.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Checking for empty strings or lists

    PLease, please don't define a constant for 0... it is completely unnecessary. 0 is not a "magic number", it is perfectly clear, as is "" for an empty string. Some literals have obvious meaning and there is no reason to use a constant. Using String.Empty is fine, just be consistent. String.Empty used to be interned whereas "" was not, but that has not been true for some time now and they are equivalent.

    As Arjay points out, use IsNullOrEmpty or IsNullOrWhitespace if that's what you actually want to check, but if you really on want to check against "" then use that or String.Empty.
    Last edited by BigEd781; July 22nd, 2011 at 01:22 PM.

  5. #5
    Join Date
    Jul 2011
    Posts
    2

    Re: Checking for empty strings or lists

    I agree, I think that (List.Items.Count() > 0) is perfectly fine.

    I do prefer string.empty or the String.IsNullOrEmpty() method for checking strings though - as it is more clear when reading code what your intention was in my opinion at least..

    If you were trying to describe different states of an object with integers, I would recommend using an enumeration - but if you just want to say "Do I have anything in this collection?" - I think specifically using '0' is acceptable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center