|
-
February 26th, 2009, 11:49 AM
#1
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.
-
February 26th, 2009, 12:13 PM
#2
Re: Why this is not working?
 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;
The difficulty is that you have no idea how difficult it is.
.Net 3.5/VS 2008
-
February 26th, 2009, 12:20 PM
#3
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
-
February 26th, 2009, 12:21 PM
#4
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
-
February 26th, 2009, 12:46 PM
#5
Re: Why this is not working?
Thanks guys!
It's working.
-
February 26th, 2009, 08:07 PM
#6
Re: Why this is not working?
If you use a NumericUpDown control you will never have this problem
-
February 27th, 2009, 05:18 AM
#7
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. 
-
February 27th, 2009, 11:52 AM
#8
Re: Why this is not working?
 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.
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
-
February 27th, 2009, 12:16 PM
#9
Re: Why this is not working?
 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.
-
February 27th, 2009, 12:19 PM
#10
Re: Why this is not working?
 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.
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.
-
February 27th, 2009, 01:50 PM
#11
Re: Why this is not working?
 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(...)
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
-
February 27th, 2009, 02:45 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|