473,672 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

empty string paramters rather than nulls

Is there any way (maybe using an attribute) that I can force strings or
other reference parameters to always have an instance even if they are
blank? Ex:

[WebMethod]
public string DoSomething(str ing Text)
{
if (Text != null) /// i would like to avoid checks like this. they get
to be annoying when passing a lot of strings
DoWork();
}

If a blank string is passed to this function, the framework won't create a
string instance. I would prefer that it did so I wouldn't have to pepper my
code with != null checks.
May 4 '06 #1
9 1371
Try the following code

[WebMethod]
public string DoIt(string text)
{
if (text.Length != 0)
DoThat ();
return ...;
}

Have a fun!

Valentin

Do not hesitate to contact me!

www.wwv-it.eu
va************* @t-online.de

"Jacob" wrote:
Is there any way (maybe using an attribute) that I can force strings or
other reference parameters to always have an instance even if they are
blank? Ex:

[WebMethod]
public string DoSomething(str ing Text)
{
if (Text != null) /// i would like to avoid checks like this. they get
to be annoying when passing a lot of strings
DoWork();
}

If a blank string is passed to this function, the framework won't create a
string instance. I would prefer that it did so I wouldn't have to pepper my
code with != null checks.

May 10 '06 #2
Wouldn't that be calling null.Length if someone passed a blanks string and
generating an exception?
Try the following code

[WebMethod]
public string DoIt(string text)
{
if (text.Length != 0)
DoThat ();
return ...;
}

May 10 '06 #3
The previous code generate no exception for blanks strings.

The following code does not generate any exception even when the input
string is a null string.

[WebMethod]
public string DoIt(string text)
{
try
{
if (text.Length != 0)
{
DoThat ();
return "OK";
}
else return "Length = 0";
}
catch
{
return "Null string";
}

}

Have a fun!

Valentin

Do not hesitate to contact me!

www.wwv-it.eu
va************* @t-online.de
"Jacob" wrote:
Wouldn't that be calling null.Length if someone passed a blanks string and
generating an exception?
Try the following code

[WebMethod]
public string DoIt(string text)
{
if (text.Length != 0)
DoThat ();
return ...;
}


May 10 '06 #4
A little bit improving

[WebMethod]
public string DoIt(string text)
{
int i;
try
{
i = text.Length;
if (i != 0)
{
DoThat ();
return i.ToString ();
}
else return "0";
}
catch
{
return "Null string";
}
"Valentin" wrote:
The previous code generate no exception for blanks strings.

The following code does not generate any exception even when the input
string is a null string.

[WebMethod]
public string DoIt(string text)
{
try
{
if (text.Length != 0)
{
DoThat ();
return "OK";
}
else return "Length = 0";
}
catch
{
return "Null string";
}

}

Have a fun!

Valentin

Do not hesitate to contact me!

www.wwv-it.eu
va************* @t-online.de
"Jacob" wrote:
Wouldn't that be calling null.Length if someone passed a blanks string and
generating an exception?
Try the following code

[WebMethod]
public string DoIt(string text)
{
if (text.Length != 0)
DoThat ();
return ...;
}


May 10 '06 #5
I would simply check for text == null rather than catching an exception. It
is faster and better expresses the intent of the code. This code also
returns "Null string" if DoThat() throws any exception.

Exceptions should not be used for control flow, but to catch unanticipated
problems.

"Valentin" <Va******@discu ssions.microsof t.com> wrote in message
news:60******** *************** ***********@mic rosoft.com...
A little bit improving

[WebMethod]
public string DoIt(string text)
{
int i;
try
{
i = text.Length;
if (i != 0)
{
DoThat ();
return i.ToString ();
}
else return "0";
}
catch
{
return "Null string";
}

May 10 '06 #6
Jacob wants even to avoid the null / not null test (Text == null / Text !=
null). See his question.
There is no restriction how to use the exceptions. Of course they must not
be redundant to system-defined exceptions. But a custom exception can
substitute, for instance, a test of null or 0. What is the problem? In both
cases the method will return with a message string indicating an error.
Sometimes a custom exception (try … operation …catch …error message) is more
simply than a usual test followed by a message error (if true) or by the
operation (if false) .
My code was just an example. Of course DoThat could throw exceptions, if the
method itself is not provided with all tests. But this was not the problem.
See Jacobs question.
Any the DoThat method could be also included in a try …. catch … finally
sequence.
But again this was not the problem.

Valentin
IT-Bachelor
Microsoft Certified Professional
"Mark Wilden" wrote:
I would simply check for text == null rather than catching an exception. It
is faster and better expresses the intent of the code. This code also
returns "Null string" if DoThat() throws any exception.

Exceptions should not be used for control flow, but to catch unanticipated
problems.

"Valentin" <Va******@discu ssions.microsof t.com> wrote in message
news:60******** *************** ***********@mic rosoft.com...
A little bit improving

[WebMethod]
public string DoIt(string text)
{
int i;
try
{
i = text.Length;
if (i != 0)
{
DoThat ();
return i.ToString ();
}
else return "0";
}
catch
{
return "Null string";
}


May 11 '06 #7
From: "Valentin" <Va******@discu ssions.microsof t.com>
Jacob wants even to avoid the null / not null test (Text == null / Text
!=
null). See his question.
You're right. My mistake.
There is no restriction how to use the exceptions.
There are accepted practices, however. This isn't one of them. And for good
reason - throwing an exception is much slower than performing a comparison.
Catching an exception is much less clear, as well.
My code was just an example. Of course DoThat could throw exceptions, if
the
method itself is not provided with all tests. But this was not the
problem.
This is a good example of why one should always catch the most derived
Exception possible.
Any the DoThat method could be also included in a try .. catch . finally
sequence.

What a waste, just to avoid a comparison to null!

In any event, if the goal is to avoid null or empty strings,
String.IsNullOr Empty() should be used. Code should say what it means to do.

///ark
May 11 '06 #8
The previous code generate no exception for blanks strings.

But it appears (at least in my test) that when someone passes a blank string
parameter to my web service

e.g. <SomeStringPara meter/>

the receiving web service doesn't create a blank 0 length string instance.
Instead it receives a null string reference. Which means I then have to be
putting in checks for the null to avoid the "object reference not set to an
isntance of an object" errors that wouldn't be raised if there was a blank
instance.

Ideally I wanted to know if you could set some sort of pre condition
attribute so that the framework would always create an instance (even if it
was empty) when passing in strings, or arrays, or other reference objects.

I think the quickest thing for me to do is to add this type of fix at the
top of the function:

if (stringparamete r == null) stringparamter = "";

But it seems a little silly.

May 11 '06 #9
I am working with Microsoft Visual C# .NET 2003
It has not the method String.IsNullOr Empty().
And now it is really a waste this discussion.

Valentin
"Mark Wilden" wrote:
From: "Valentin" <Va******@discu ssions.microsof t.com>
Jacob wants even to avoid the null / not null test (Text == null / Text
!=
null). See his question.


You're right. My mistake.
There is no restriction how to use the exceptions.


There are accepted practices, however. This isn't one of them. And for good
reason - throwing an exception is much slower than performing a comparison.
Catching an exception is much less clear, as well.
My code was just an example. Of course DoThat could throw exceptions, if
the
method itself is not provided with all tests. But this was not the
problem.


This is a good example of why one should always catch the most derived
Exception possible.
Any the DoThat method could be also included in a try .. catch . finally
sequence.

What a waste, just to avoid a comparison to null!

In any event, if the goal is to avoid null or empty strings,
String.IsNullOr Empty() should be used. Code should say what it means to do.

///ark

May 11 '06 #10

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

Similar topics

3
4950
by: Paul M. | last post by:
Hi, I am trying to populate 3 paramters in an asp .net (vb) page redirect, the first one is ok ok and gets populated by the other two get inserted into the url for the redirect as empty strings! Anyone got any clues?? The code is below and I have checked that the variables beginning with "g_str" are all populated before the code is called. I think its something to do with the "&" in the "&Title" and "&Version", do I need to do something to...
8
10405
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have is not much help here either. Googling has given me a little help. This is my current understanding -- I would appreciate any comments or corrections... "" -- this means an empty string when applied to String data type, and also to Variant...
7
1799
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(); Console.WriteLine(count++ + " '" + d + "' --> " + d.Length); Sometimes "d" should be empty, and sometimes it's a date string. And in the VS.NET watch window, it's indeed empty. Except that my diagnostic line sometimes prints just the number...
1
2991
by: Reza | last post by:
Hi I have a column in my datagrid that can have values of null at times. I am not assigning any value to it, if it is coming from Database empty. Now, the problem is I guess the datetime variables have a default value. Thus shows an undesirable value of 1/1/0001 How do I go about not showing anything when the value is null like this Thanks in advance Reza
4
2474
by: Randall Parker | last post by:
I am designing a database schema. It happens to be in MySQL but I'm trying to keep it portable to other databases should the project grow. Anyway, suppose you have VARCHAR fields and will be using ASP.Net and ADO.Net. In your experience does it make more sense to allow NULL values for VARCHAR fields or will they behave well and get set to zero length strings when a user doesn't fill in a text field? I'm wondering if I should do:
3
1920
by: Zach | last post by:
Hello, This might be a rather basic question, but I've tried a few things and I can't really find a solution as elegant as what I'd like for this problem. The situation is this - I have a file that's written to disk in a binary format. Basically it's a bunch of records, one after the other, where each record has the following format : 18 bytes, 6 bytes which are all zero, 4 bytes which represent a UInt32. The first 18 bytes of each...
7
63113
by: tomlebold | last post by:
Are the following two validation the same: 1) IsNull(Me.ColumnName) and Me.ColumnName = "" 2) Me.ColumnName """ It would seem to be better to use: Me.ColumnName """
2
3088
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post by:
Hi, I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper around some C++ LIBS. I've created some classes that contains a pointer to the LIB classes and everthing seems to compile well. The problem is getting a std::string from System::String and still preserving the nulls. System::String is a base64 encoded string of a series of bytes. When I convert it back to a string from base64 representation, the NULLs inside the...
4
3032
by: Jay | last post by:
For a column that contains a string (let's say varchar), is there any performance advantage in not allowing nulls, and using an empty string ("") to instead?
0
8404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8931
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8608
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8680
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.