473,750 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I use the null value as the parameter?

Jet
Hi all,
I had a code like the follow:
class A{
...
public void get(int i,params OleDbParameter[] p)
{
if((p!=null)||( p.Length>0))
{
// Do some work
}
else
{
// Do another work
}

}
....
}
And in other class I had create the instance which include this method , and
I had not the OleDbParameter[] parameter tranfer to this method.And I try to
do it like this to call the method:
....
A _a=new A();
int i=100;
_a.get(i,(OleDb Parameter[])null);
....

But when it run to the method it throw me a "NullRefranceEx ception"
How can I solve this problem? Why I can't give it the null value as the
parameter tranfer to the get() method? I also declare the null value as the
type OleDbParameter[].
Nov 17 '05 #1
5 3143

Jet wrote:
Hi all,
I had a code like the follow:
class A{
...
public void get(int i,params OleDbParameter[] p)
{
if((p!=null)||( p.Length>0))
Do you actually mean

if((p!=null)&&( p.Length>0))

?

{
// Do some work
}


Everything else looks OK.

--
Larry Lard
Replies to group please

Nov 17 '05 #2
If a parameter is modifed with the "params" keyword, you can completely ignore the parameter, however, the framework guarantees that
the Array will be initialized and empty:

public void MyMethod(int i, params OleDbParameter[] p)
{
if (p.Length > 0)
{
// Todo: work
}
}

MyMethod( 100 ); // ignores the parameter array argument

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"Jet" <li******@126.c om> wrote in message news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Hi all,
I had a code like the follow:
class A{
...
public void get(int i,params OleDbParameter[] p)
{
if((p!=null)||( p.Length>0))
{
// Do some work
}
else
{
// Do another work
}

}
...
}
And in other class I had create the instance which include this method , and I had not the OleDbParameter[] parameter tranfer to
this method.And I try to do it like this to call the method:
...
A _a=new A();
int i=100;
_a.get(i,(OleDb Parameter[])null);
...

But when it run to the method it throw me a "NullRefranceEx ception"
How can I solve this problem? Why I can't give it the null value as the parameter tranfer to the get() method? I also declare the
null value as the type OleDbParameter[].

Nov 17 '05 #3
Dave <NO*********@do tcomdatasolutio ns.com> wrote:
If a parameter is modifed with the "params" keyword, you can
completely ignore the parameter, however, the framework guarantees
that the Array will be initialized and empty:

public void MyMethod(int i, params OleDbParameter[] p)
{
if (p.Length > 0)
{
// Todo: work
}
}

MyMethod( 100 ); // ignores the parameter array argument


No, it's not guaranteed to be non-null - the caller can specify a null
value as the OP actually had. I believe that Larry nailed the problem.

Here's an example of a params parameter being null:

using System;

class Test
{
static void Main()
{
SomeMethod((str ing[])null);
}

static void SomeMethod(para ms string[] names)
{
if (names==null)
{
Console.WriteLi ne ("I'm null!");
}
else
{
foreach (string name in names)
{
Console.WriteLi ne (name);
}
}
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Yes, you are right.

I tried and it seems that just passing "null", without casting, works too.

So, it gaurantees that the value will be initialized empty unless the caller explicitly passes null.

Sound good?

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message news:MP******** *************** *@msnews.micros oft.com...
Dave <NO*********@do tcomdatasolutio ns.com> wrote:
If a parameter is modifed with the "params" keyword, you can
completely ignore the parameter, however, the framework guarantees
that the Array will be initialized and empty:

public void MyMethod(int i, params OleDbParameter[] p)
{
if (p.Length > 0)
{
// Todo: work
}
}

MyMethod( 100 ); // ignores the parameter array argument


No, it's not guaranteed to be non-null - the caller can specify a null
value as the OP actually had. I believe that Larry nailed the problem.

Here's an example of a params parameter being null:

using System;

class Test
{
static void Main()
{
SomeMethod((str ing[])null);
}

static void SomeMethod(para ms string[] names)
{
if (names==null)
{
Console.WriteLi ne ("I'm null!");
}
else
{
foreach (string name in names)
{
Console.WriteLi ne (name);
}
}
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
Dave <NO*********@do tcomdatasolutio ns.com> wrote:
Yes, you are right.

I tried and it seems that just passing "null", without casting, works
too.
Right. I like to make it explicit, so it's clear I'm not trying to pass
a single string parameter.
So, it gaurantees that the value will be initialized empty unless the
caller explicitly passes null.

Sound good?


Yup.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

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

Similar topics

2
58107
by: Chris Herring | last post by:
Hi there: Well, let me start off by saying that I am a Visual Studio drag and drop weenie, not a real programmer. So I tend to get confused when things do not look like the instructions said they were going to look :) . Here is my current dilemma: I am having a strange problem with my ASP.NET application. I am building the application using Visual Basic .Net in Visual Studio.NET 2003. Lately, I have frequently been getting the following...
2
19346
by: PK | last post by:
Hi, I have an application that opens a Crystal report document and passes in a value to a parameter in the report (pointing to an Oracle DB). However, if I want to pass a "null" value to retrieve all records that have no data for the specified field that the parameter is a condition of (ie. SELECT * from <table> where <field> is null"), it returns nothing. I think CR is building the SQL statement like "SELECT * from <table> where <field>...
4
6001
by: Andrew Baker | last post by:
I have the following code that calles a stored proc in SQLServer. When the output parameter @custref is null (System.DBNull) I cant seem to find a test for this and I get an exception. I know I could coalesce the stored proc, but I would like to know if the value is null. How do you test for null in the returned parameter of a stored proc? TIA Andrew. Dim retstr As String = ""
17
4534
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are selected into 2 different SP variables and compared for equal. They are both NULL, but do not compare as equal. When the Not NULL columns (SALARY) are compared, they do compare as equal.
5
5512
by: John | last post by:
I just cannot manage to perform a SELECT query with NULL parameter... My CATEGORY table does have one row where TCATEGORYPARENTID is null (real DB null value). TCATEGORYID and TCATEGORYPARENTID are uniqueidentifier columns. My questions: - is Type="Object", below, necessary? - what shall I specify for DefaultValue in my function? I tried everything.
2
2144
by: Weeble | last post by:
Back in C++, I had a type that looked something like this (entering from memory so this might not quite compile): template <class T> class NotNull { private: T* value; public: NotNull(T* ptr)
11
3265
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed in the constructor is destroyed. I have implemented a Dispose(), Dispose(bool), and ~Finalize...
76
4704
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the pointers are allowed to be null, while references must refer an existing variable of required type. The null is normally used for making optional parameters. But there is no way to pass null reference in C#. Something is missing.
4
2906
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field is not a null value. I am not using any code behind (C#) to bind the data or manipulate the data. I have read that when there is a null value in the database that there is no record in the "dataset". Can anyone show me how to bind a value in the...
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
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
9568
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...
1
9335
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
9256
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
6801
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.