Help - Creating a function to deal with null parameters

  • Thread starter Thread starter pjarvis
  • Start date Start date
P

pjarvis

Hi guys,

Say for example i have a hypothetical function taking 4 parameters such as the one below:

public void InsertRow(char gender, double decimalAge, double height, double weight)
{
//Adds data into a MySQL database
}

If i have a piece of data missing such as height, but i still want to call the function that will add the other parameters into the database, how would i do it? I'd like to pass
a 'null' to the function, is there any way of achieving this without getting compiler errors?

Thanks in advance

Paul
 
In .NET 2.0, you can use

public void InsertRow(char? gender, double? decimalAge, double? height,
double? weight)
{
if( gender == null )
{
//not provider
}
else
{
field = (char) gender;
}
....
 
Say for example i have a hypothetical function taking 4 parameters
such as the one below:

public void InsertRow(char gender, double decimalAge, double height, double weight)
{
//Adds data into a MySQL database
}

If i have a piece of data missing such as height, but i still want to
call the function that will add the other parameters into the
database, how would i do it? I'd like to pass
a 'null' to the function, is there any way of achieving this without
getting compiler errors?

Well, char and double values can't be null. You options are:
1) On .NET 2.0, use nullable types: char? and double?
See http://pobox.com/~skeet/csharp/csharp2/nullable.html for more on
this

2) Otherwise, have particular char and double values that you wouldn't
normally actually use, and have those be "special" ways of representing
null. It's a bit hacky, admittedly...
 

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