473,513 Members | 2,428 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(string 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 1358
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(string 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******@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.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******@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.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******@discussions.microsoft.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.IsNullOrEmpty() 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. <SomeStringParameter/>

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 (stringparameter == 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.IsNullOrEmpty().
And now it is really a waste this discussion.

Valentin
"Mark Wilden" wrote:
From: "Valentin" <Va******@discussions.microsoft.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.IsNullOrEmpty() 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
4939
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!...
8
10394
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...
7
1791
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();...
1
2980
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...
4
2463
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...
3
1911
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...
7
63079
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
3079
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...
4
3023
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
7160
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...
0
7537
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...
1
7099
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...
1
5086
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...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
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.