473,378 Members | 1,421 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,378 software developers and data experts.

replace null with ""

Hi,

I would like to have a public string which will return a "" if the parameter
is a Null string, would someone tell me what's wrong with my code?

public string NoNull(string strNoNull)
{
if (strNoNull==null)
{
return "";
}
else
{
strNoNull=strNoNull;
}

Thanks for help.

Jason
Nov 17 '05 #1
6 18068
Hi Jason, try this...

public string NoNull( string arg )
{
if ( arg == null )
return "";
else
return arg;
}

Things I can see wrong with it - not all code paths return a value. And this
:
else
{
strNoNull=strNoNull;
}
What do you think this is doing?

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:ep**************@TK2MSFTNGP11.phx.gbl... Hi,

I would like to have a public string which will return a "" if the parameter is a Null string, would someone tell me what's wrong with my code?

public string NoNull(string strNoNull)
{
if (strNoNull==null)
{
return "";
}
else
{
strNoNull=strNoNull;
}

Thanks for help.

Jason

Nov 17 '05 #2
Tim Haughton wrote:
public string NoNull( string arg )
{
if ( arg == null )
return "";
else
return arg;
}


If you're on .NET 2, try the new ?? operator to do the same thing:

return arg ?? "";

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3
"Oliver Sturm" <ol****@sturmnet.org> a écrit dans le message de news:
xn****************@msnews.microsoft.com...
public string NoNull( string arg )
{
if ( arg == null )
return "";
else
return arg;
}


If you're on .NET 2, try the new ?? operator to do the same thing:

return arg ?? "";


....or even the good old ?: syntax

public string NoNull( string arg )
{
return arg == null ? "" : arg;
}

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #4
Tim Haughton wrote:
Hi Jason, try this...

public string NoNull( string arg )
{
if ( arg == null )
return "";
else
return arg;
}


Why the else?

if (arg == null)
return "";
return arg;

Still better is Joanna Carters option of ?:
Nov 17 '05 #5
I agree with Joanna's code. I do it for almost all string properties because
as the guidelines state a string should really never be null because this
throws people off. Unless a null string has a different meaning than an
empty string it pays to be nice. My only recommendation would be to switch
the boolean operator because you assume that normally it is not null so you
save a jump instruction (might actually be optimized by the compiler). So...

public string NoNull ( string arg )
{
return arg != null ? arg : "";
}

Michael Taylor - 9/5/05

"Jimbo" wrote:
Tim Haughton wrote:
Hi Jason, try this...

public string NoNull( string arg )
{
if ( arg == null )
return "";
else
return arg;
}


Why the else?

if (arg == null)
return "";
return arg;

Still better is Joanna Carters option of ?:

Nov 17 '05 #6
Hi, I was just browsing through this thread and noticed this comment...
My only recommendation would be to switch
the boolean operator because you assume that normally it is not null so you
save a jump instruction (might actually be optimized by the compiler).
Over the years I've often wondered about the best way to handle if.. else..
for error conditions - specifically whether to test for the erroneous
condition, or whether to test for the correct condition. The conclusion I've
reached is one which I can apply to every conditional statement - prefer the
simplest form of conditions which is correct in the circumstances. For
instance, prefer == to !=, prefer > to <=, etc. This cut's down on double
negatives, which I think make code more difficult to read. eg. "if a !=
b..else.." - if we think, we can see that the else is the "a == b" case, but
it's not as readable as "if a == b ... else ..", and given that conditions
can be nested, we should aim build up a sequence of positive, rather than
negative, conditions.

The potential saving of a jump statement, in the normal case, by changing
the condition, is an interesting issue. Thanks for pointing that out.

M2CW

Javaman

"TaylorMichaelL" wrote:
I agree with Joanna's code. I do it for almost all string properties because
as the guidelines state a string should really never be null because this
throws people off. Unless a null string has a different meaning than an
empty string it pays to be nice. My only recommendation would be to switch
the boolean operator because you assume that normally it is not null so you
save a jump instruction (might actually be optimized by the compiler). So...

public string NoNull ( string arg )
{
return arg != null ? arg : "";
}

Michael Taylor - 9/5/05

"Jimbo" wrote:
Tim Haughton wrote:
Hi Jason, try this...

public string NoNull( string arg )
{
if ( arg == null )
return "";
else
return arg;
}


Why the else?

if (arg == null)
return "";
return arg;

Still better is Joanna Carters option of ?:

Nov 17 '05 #7

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

Similar topics

6
by: vigi98 | last post by:
Hello all, Can someone confirme that this: var strURLpiece = "UK & Ireland"; strURLpiece.replace("&", "%26"); replaces all occurrence of the character & by %26 in strURLpiece, ie that...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
10
by: dcrespo | last post by:
Hi all, How can I replace all None values with the string 'Null' in a dictionary? For example: convert this: a = {'item1': 45, 'item2': None} into this:
3
by: asd987 | last post by:
Hi, Can anyone tell me if "replace" is supported by Access 97? I use the Dutch version and get the errormessage "sub or function not supported". Or is the Professional Edition needed? Thanks.
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
2
by: ad | last post by:
Hello, if I try this ((StringBuilder)sb).Replace("a", "aa"); I get OutOfMemoryException raised if sb contains at least one "a"... It seems like StringBuilder 'seeker' doesn't move to the end...
6
by: Marty | last post by:
Hi, I would like to replace "\r\n" by "_" within a specific string. I tried : strMyString.Replace('\r', '_'); strMyString.Replace('\n', '_'); or...
6
by: Dean Slindee | last post by:
Private NameLastFirst as object = "Public, John Q." NameLastFirst = Replace(LastName, "'", "''") If NameLastFirst contains "Public, John Q." before the above Replace, it will contain "Public"...
0
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and...
1
by: jx2 | last post by:
lets say i've got a str="john michael luise+marry" i need to replace all ocurrance of name with name+surname eg str2= "johnBrown michealBrown luiseBrown+marryBrown" i gues i need something like...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.