CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 43 of 43
  1. #31
    Join Date
    May 2007
    Posts
    1,546

    Re: Best way to check if a string is empty

    Quote Originally Posted by dee-u View Post
    We should agree that it boils down to the personal inclination of a developer or organization, some would prefer performance over readability and readable for some can be not that readable to others, do you agree?
    Except that in either case, 'performance' doesn't come into it. It's another premature optimisation If you're worried about performance differences between String.IsNullOrEmpty and string.Length == 0, you're worrying about completely the wrong things. What you should be worried about is whether or not you want to allow a null or empty string. If you don't, you should use String.IsNullOrEmpty.

    Of course, the source of your string should be part of your 'view', which should be completely disconnected from your model and controller (MVC pattern), so your model will be doing all the validation, which means you can't rely on the source of your data being a TextBox, which means you can't rely on it being non-null.

    Finally, near in mind that you validate before you store. Validation will done a hell of a lot less than the variable will be used, so even if you do 'mega optimise' your validation methods you're not going to get a real world performance boost.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  2. #32
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Best way to check if a string is empty

    I am not worried about the performance, its just that using .Length has its merits and should not be avoided like a plague, isn't it? And I am not worrying at all, it has become a second nature and to be honest I have not yet encountered a scenarion wherein a string has the value of null, I was not even aware of it until it has been mentioned in this thread.

  3. #33
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Best way to check if a string is empty

    Quote Originally Posted by dee-u View Post
    I am not worried about the performance, its just that using .Length has its merits and should not be avoided like a plague, isn't it?
    I'd avoid it (or any other approach that uses a string instance method to determine whether the string is valid).

    As I and others have mentioned earlier, you may consider validating the string data before storing it into the string variable. Then all your checks can be reduced to String.IsNullOrEmpty calls.

    To illustrate, consider a situation where a string is valid if it contains alpha/numeric chars, no leading or trailing spaces, is not null and cannot start with a 'z' character.

    If you validate (and/or trim) a string such as this prior to storing it into a variable, it just makes life easier.

    Would you rather perform a validity check like this:
    Code:
    if( String.IsNullOrEmpty( str ) ) 
    {
      //...
    }
    or
    Code:
    if( str != null && str.Trim( ).Length != 0 && str.Trim( )[ 0 ] != 'z' )
    {
      //...
    }

  4. #34
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Best way to check if a string is empty

    Hmmmnnn... That makes me wonder, if we need to pass something to a Business Logic Layer which is supposed to validate rules, are we supposed to validate them first in the GUI before sending them over to the BLL? I usually just do this in the GUI:
    Code:
    asset_bl.BLOCK_NO = textBLOCK_NO.Text;
    and just let my BLL do the validation. Do you mean to say I need to validate the contents of textBLOCK_NO.Text first before setting them to asset_bl.BLOCK_NO?

  5. #35
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Best way to check if a string is empty

    It depends based on the type of feedback you wish to give your user.

    If you validate in the UI, the user can get immediate feedback which is often quite nice.

    Validating in the business object is good too; especially if the business object is accessed by something other than the UI.

    So it seems maybe the logic should be in both places?

    Well not really. Consider using the validation components in the enterprise library. These allow you to create validation attributes on the business object properties and then perform validation external to the business object. For UI's (like WinForms and WPF) this validation can be automatically connected so the user receives immediate feedback.

    The validators can also be called on the business object level. With the entlib validator approach the validation logic is only in one spot but the validation can occur on different layers.

    One last comment: You might consider following the C# .Net style guidelines/naming conventions. This will make your code easier to read for folks familiar with C#.

  6. #36
    Join Date
    Apr 2006
    Posts
    220

    Re: Best way to check if a string is empty

    Quote Originally Posted by marceln View Post
    Code:
    if(String.IsNullOrEmpty(str)) 
    {
    //...
    }
    It is only for C# 3. not earlier versions. At least not for Framework 1.1.

  7. #37
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Best way to check if a string is empty

    Quote Originally Posted by nabeelisnabeel View Post
    It is only for C# 3. not earlier versions. At least not for Framework 1.1.
    It's available on .Net 2.0 and above. Hopefully no one is forced to code in 1.1 or earlier.

  8. #38
    Join Date
    Apr 2006
    Posts
    220

    Re: Best way to check if a string is empty

    Pointing above mentioned point was necessary because we do not know in which framework version the topic starter is developing his application.

  9. #39
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Best way to check if a string is empty

    .NET version 1.1 is depricated, so I think we can safely assume that anyone posting here is using version 2.0 or higher. Going from 1.1 to 2.0 would not break anyone's code.

  10. #40
    Join Date
    Apr 2006
    Posts
    220

    Re: Best way to check if a string is empty

    Quote Originally Posted by BigEd781 View Post
    .NET version 1.1 is depricated, so I think we can safely assume that anyone posting here is using version 2.0 or higher. Going from 1.1 to 2.0 would not break anyone's code.
    Correction:

    .NET version 1.1 is in the process of depreciation, not depreciated yet.

  11. #41
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Best way to check if a string is empty

    Quote Originally Posted by nabeelisnabeel View Post
    Correction:

    .NET version 1.1 is in the process of depreciation, not depreciated yet.
    So..if you walked into a hotel with cracks in the walls and stained rugs, and no one had any intention of fixing it, would you say it was deprecated, or in the process of deprecation? In practice, it is an outdated API that no one should be using, but it is true that it has not yet been fully left behind in the dust (yet).

  12. #42
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Best way to check if a string is empty

    Quote Originally Posted by nabeelisnabeel View Post
    Correction:

    .NET version 1.1 is in the process of depreciation, not depreciated yet.
    Officially:
    Quote Originally Posted by Microsoft Product LifeCycle
    Retired .NET Framework 1.1 7/10/2003 -10/14/2008
    Over two months past EOS
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #43
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Best way to check if a string is empty

    i think writing code base on .Net 1.1 is a horrible mistake because there are radical differences between 1.1 and 2.0; but not so many diffrence between 2 and 3.5
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Page 3 of 3 FirstFirst 123

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured