How can I use the null value as the parameter?

  • Thread starter Thread starter Jet
  • Start date Start date
J

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,(OleDbParameter[])null);
....

But when it run to the method it throw me a "NullRefranceException"
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[].
 
Jet said:
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.
 
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 said:
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((string[])null);
}

static void SomeMethod(params string[] names)
{
if (names==null)
{
Console.WriteLine ("I'm null!");
}
else
{
foreach (string name in names)
{
Console.WriteLine (name);
}
}
}
}
 
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
[email protected]
-----------------------------------------------------------------------
Jon Skeet said:
Dave said:
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((string[])null);
}

static void SomeMethod(params string[] names)
{
if (names==null)
{
Console.WriteLine ("I'm null!");
}
else
{
foreach (string name in names)
{
Console.WriteLine (name);
}
}
}
}
 
Dave said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top