CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2008
    Posts
    91

    Why this is not working?

    Hi all,

    I am using the ?: operator to check if a number wasn't entered in a textbox. If nothing was enetered, I want to default it to "0". The reason is because I need to convert the value to a numeric value that will be stored in a database. Below is the code I am using.

    Code:
    string test =  this.rtxtProtqty.Text == null ? "0" : this.rtxtProtqty.Text;
    Thanks in advance.

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Why this is not working?

    Quote Originally Posted by admdev View Post
    Hi all,

    I am using the ?: operator to check if a number wasn't entered in a textbox. If nothing was enetered, I want to default it to "0". The reason is because I need to convert the value to a numeric value that will be stored in a database. Below is the code I am using.

    Code:
    string test =  this.rtxtProtqty.Text == null ? "0" : this.rtxtProtqty.Text;
    Thanks in advance.
    rtxtProtqty.Text never is gonna be null. You can check it by
    Code:
    string test = (this.rtxtProtqty.Text.Trim().Length == 0) ? "0" : this.rtxtProtqty.Text;
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  3. #3
    Join Date
    May 2007
    Posts
    811

    Re: Why this is not working?

    There is alreay method to cover both cases
    Code:
    string test =  String::IsNullOrEmpty(this.rtxtProtqty.Text) ? "0" : this.rtxtProtqty.Text;
    Last edited by STLDude; February 26th, 2009 at 12:27 PM. Reason: Had arguments wrong

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Why this is not working?

    You can also you the String.IsNullOrEmpty method

    Code:
    string test = (String.IsNullOrEmpty(this.rtxtProtqty.Text.Trim()) ? "0" : this.rtxtProtqty.Text );

    EDIT : sorry for the double reply. Had the same idea at the same time as STLDude

  5. #5
    Join Date
    Sep 2008
    Posts
    91

    Re: Why this is not working?

    Thanks guys!

    It's working.

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Why this is not working?

    If you use a NumericUpDown control you will never have this problem
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Why this is not working?

    Or generally, you can set a mask if the control you are using does support it. For example, in case of DevExpress it is like this: this.txtNotificationLimitAfterDeal.Properties.Mask.EditMask = "#,#############0";
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Why this is not working?

    Quote Originally Posted by STLDude View Post
    There is alreay method to cover both cases
    Code:
    string test =  String::IsNullOrEmpty(this.rtxtProtqty.Text) ? "0" : this.rtxtProtqty.Text;
    Yes you can use this but
    a) you need to have VS 2008 as the 2.0 framework doesn't have this method and
    b) its totally useless. The TextProperty of a Textbox never is null. No case. If ther is nothing its string.Empty.
    ------
    edited :
    point a) is not correct see following posts.
    Last edited by JonnyPoet; February 27th, 2009 at 01:53 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  9. #9
    Join Date
    May 2007
    Posts
    811

    Re: Why this is not working?

    Quote Originally Posted by JonnyPoet View Post
    Yes you can use this but
    a) you need to have VS 2008 as the 2.0 framework doesn't have this method...
    Simply not true, here.

  10. #10
    Join Date
    May 2007
    Posts
    1,546

    Re: Why this is not working?

    Quote Originally Posted by JonnyPoet View Post
    its totally useless. The TextProperty of a Textbox never is null. No case. If ther is nothing its string.Empty.
    It's only 'totally useless' if you tie your application to only ever work the the TextBox.Text property. Any proper separation of frontend and backend would require the null check as well as the empty check.

    Secondly, if null or empty are not valid inputs, then you should always use String.IsNullOrEmpty so if things do change in the future, you're still covered.
    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.

  11. #11
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Why this is not working?

    Quote Originally Posted by STLDude View Post
    Simply not true, here.
    Sorry , my fault. But I did a quick try using it in a method and got an compilation error, so I thought its maybe up to 2008 as I have 2005 here on this machine. Now as you showed the reference I tried again and looked exactly what the errormessage told me . 'The namespacequalifier'::' is not allowed at this place.' Now I tried String.IsNullOrEmpty(...) and this works.
    So this is in obviously included in framework 2.0 but you need to use String.IsNullOrEmpty(...) instead of String:: IsNullOrEmpty(...)
    Last edited by JonnyPoet; February 27th, 2009 at 01:57 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  12. #12
    Join Date
    May 2007
    Posts
    811

    Re: Why this is not working?

    Sorry about ::, C++ came through too much, yes it should be .

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