473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Empty strings

Is there any way to express this more concisely?

if (someString == null || someString == "")
throw new ArgumentException("String must have something in it."); // or
something

What would be perfect would be someString.IsEmpty(), but MS didn't see fit
to include that particular one. Maybe there's something just as good. ??

Chris
Nov 15 '05 #1
4 8912
> What would be perfect would be someString.IsEmpty(), but MS didn't see fit
to include that particular one. Maybe there's something just as good. ??


It would have be a static method, something like String.IsEmptyOrNull(string
str)
because someString.IsEmpty() will throw a null reference exception if
someString is null.

As the MS guys did not provide it, you will have do it (we did). You just
have to write some kind of StringHelper class with this static method in it.

Bruno.
Nov 15 '05 #2
Chris,

You should probably change the ArgumentException to an
ArgumentNullException, just to be more concise. Otherwise, I would do as
Bruno recommends, and create your own static method.

--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Chris Capel" <ch***@nowhere.com> wrote in message
news:OJ**************@TK2MSFTNGP11.phx.gbl...
Is there any way to express this more concisely?

if (someString == null || someString == "")
throw new ArgumentException("String must have something in it."); // or something

What would be perfect would be someString.IsEmpty(), but MS didn't see fit
to include that particular one. Maybe there's something just as good. ??

Chris

Nov 15 '05 #3
He's also checking if string is zero-length - ArgumentNullException does not
quite fit in that case.

..
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:O6**************@TK2MSFTNGP12.phx.gbl...
Chris,

You should probably change the ArgumentException to an
ArgumentNullException, just to be more concise. Otherwise, I would do as
Bruno recommends, and create your own static method.

--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Chris Capel" <ch***@nowhere.com> wrote in message
news:OJ**************@TK2MSFTNGP11.phx.gbl...
Is there any way to express this more concisely?

if (someString == null || someString == "")
throw new ArgumentException("String must have something in it."); //

or
something

What would be perfect would be someString.IsEmpty(), but MS didn't see fit to include that particular one. Maybe there's something just as good. ??
Chris


Nov 15 '05 #4
Just FYI, if you try Whidbey Alpha, you will see String.IsNullOrEmpty is
added in String class.

Gang Peng
[MS]

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
5
by: Krechting | last post by:
Hi ALl, I have a code that checks if the documents in a hyperlink field are still where they should be. I use fileexist(). First I want to filter out all the hyperlink fields that are empty. I...
7
by: Clinton Pierce | last post by:
I'm calling a method in another assembly that's supposed to return a string, and possibly that string might be empty. The code looks something like this: string d = r.Field("date").ToString();...
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
9
by: VJ | last post by:
I have a property for a control ( source code not available) . The property accepts single char as value.. so I can set it to '/' or ':' or anything like that...At initialize of my From I want this...
4
by: AVL | last post by:
Hi, I read that to check for empty strings , comparing strings using String.Length == 0 is faster than String.Empty or "" . Is it true...? If so, how?
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
36
by: laredotornado | last post by:
Hi, I'm using PHP 5. I have an array of strings. What is the simplest way to remove the elements that are empty, i.e. where the expression "empty($elt)" returns true? Thanks, - Dave
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.