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[]. 5 3119
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
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..jwaonline..com
-----------------------------------------------------------------------
"Jet" <li******@126.com> wrote in message news:%2****************@TK2MSFTNGP15.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,(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[].
Dave <NO*********@dotcomdatasolutions.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((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);
}
}
}
}
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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..jwaonline..com
-----------------------------------------------------------------------
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... Dave <NO*********@dotcomdatasolutions.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((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); } } } }
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Dave <NO*********@dotcomdatasolutions.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.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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*...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |