Re: Why this is not working?
Quote:
Originally Posted by
admdev
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;
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;
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
Re: Why this is not working?
Thanks guys!
It's working.
Re: Why this is not working?
If you use a NumericUpDown control you will never have this problem
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";
Re: Why this is not working?
Quote:
Originally Posted by
STLDude
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.
Re: Why this is not working?
Quote:
Originally Posted by
JonnyPoet
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.
Re: Why this is not working?
Quote:
Originally Posted by
JonnyPoet
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.
Re: Why this is not working?
Quote:
Originally Posted by
STLDude
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(...)
Re: Why this is not working?
Sorry about ::, C++ came through too much, yes it should be .