473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Check for Nulls with Reflection?


I have severaloverloaded methods that take a multiple combinations of
parameters, some which can't be null. I don't want to use
try...finally logic all over the place to catch the nulls. I want to
create some "catch all" functionality that I can add to every method
that will sweep through all parameters and tell me if any are null.

I think that is a good use for Reflection, but I am not sure how to
proceed. Any examples?

Thanks.
Nov 15 '05 #1
5 1199
If a parameter can't be null, then enforce it.

public void SomeMethod(object a, object b) // b cannot be null
{
if (b == null)
throw new ArgumentNullException();

// do something with a and b
}

public void SomeMethod(object b) // b cannot be null
{
if (b == null)
throw new ArgumentNullException();
else
this.SomeMethod(null, b);
}
HTH;
Eric Cadwell
http://www.origincontrols.com
Nov 15 '05 #2

Hi localhost,

Thank you for posting in the community!

I think Eric's solution is the correct way to do parameter validation. You
may follow that to see if it resolves your problem.

If you have any questions or concerns, please feel free to post it in the
group. I am standing by to be of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #3

Well, I wanted to get away from having to do a lot of if... statements
as well, because with 8+ parameters on lots of methods it gets
tedious. I was hoping there was some kind of catch-all trick I could
do to see if any passed parameters were null as soon as the method
signature was touched.

On Mon, 23 Feb 2004 10:51:48 -0700, "Eric Cadwell"
<ec******@ns.insight.com> wrote:
If a parameter can't be null, then enforce it.

public void SomeMethod(object a, object b) // b cannot be null
{
if (b == null)
throw new ArgumentNullException();

// do something with a and b
}

public void SomeMethod(object b) // b cannot be null
{
if (b == null)
throw new ArgumentNullException();
else
this.SomeMethod(null, b);
}
HTH;
Eric Cadwell
http://www.origincontrols.com


Nov 15 '05 #4

Hi localhost,

Thanks very much for your feedback.

I see your concern, as a substitution, you can use try...catch approach.
You can only apply try...catch in most-parameter overloading function, then
in other less-parameter overloading function, just invoke the
most-parameter overloading function, do like this:

public void func(object p1, object p2, object p3, object p4)
{
try
{
}
catch(Exception ex)
{
}
}

public void func(object p1, object p2, object p3)
{
object p4="default value";
func(p1,p2,p3,p4);
}

public void func(object p1, object p2)
{
object p4="default value";
object p3="default value";
func(p1,p2,p3,p4);
}

I think this may meet your need.

=============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5

Hi localhost ,

Does my workaround of using try...catch meet your need?

If you still have concern, please feel free to feedback.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #6

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

Similar topics

6
by: mike | last post by:
I'm doing what I thought was a simple GROUP BY summary of fairly simple data and the my numbers aren't working out Some results are showing up <NULL> when I know the data is in the database ...
9
by: collincm | last post by:
Hi, I am trying to optimize a table for inserts. Half of the timeron cost is in the FK lookup! These tables for example CREATE TABLE FOO2( FOO2_ID INTEGER NOT NULL CONSTRAINT FOO2_PK...
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
1
by: PST | last post by:
Here's a problem I'm trying to deal with: I'm working on a Frontpage 2000 website for a boat handicapping system, built in Access 97. What I'm trying to accomplish is: The user enters a...
14
by: tshad | last post by:
I have people telling me that I should set up objects for my tables, but I am finding the Null problem makes that difficult. It isn't a big problem if you are not updating the table, but if you...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
6
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in...
6
by: mj2736 | last post by:
I created a windows form control that inherits from the standard .Net DataGridView control, to which I've added custom functionality. Instead of using the standard control in my applications, I use...
7
by: Jeff | last post by:
I need a way to do the following and cannot seem to find a solution via google. 1. Have a method from the main app to get all open forms 2. Check each open form for a public method 3. If this...
0
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,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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
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...

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.