views:

111

answers:

6

I have a method private static DataTable ParseTable(HtmlNode table) and sometimes this method has no return value then I want to make return value optional, is it possible ?

I have tried with if condition.But there is error.

How can I make return value optional for the method if possible ?

+3  A: 

You could always return null to indicate that there is no meaningful return value.

Eric
+11  A: 

You can't make the return value optional.

Your two options are to either return a null or an empty DataTable:

return new DataTable();

OR:

return null;

Which one is right? It depends on your application logic and how much null checking you are willing to do in your calling functions.

Update: (following comment)

This is how to return conditionally (assumes a variable called dataTable):

 if(gotTable)
 {
   return dataTable;
 }
 else
 {
   return null;
 }
Oded
@Oded, But how can I write return statement in if condition like if table got then return datatable else null ?
Harikrishna
OMG! you have the code in your sentence:)
Veer
@Oded,ok..I thought if we make return value conditionally it gives error.thanks...Sir..
Harikrishna
+4  A: 

That's what null is for. Your method may return no object by returning null.

Keep in mind that returning null may complicate matters for the callers as they must check for null before using the returned reference, so in some cases it may be better to return a Null Object.

Brian Rasmussen
A: 

as said above.. thats what the null return value is for..

Herter
A: 
private static void ParseTable(HtmlNode table, out DataTable data)
{
   // do the parse, fill bool gotTable 
   data = gotTable ? new DataTable() : null;
}

private static void ParseTable(HtmlNode table)
{
   ParseTable(table, out null);
}

if caller need table

DataTable data;
ParseTable(table, out data);

if not

ParseTable(table);

EDIT: I can't find how to implement optional out/ref parameters. So maybe it's impossible until .NET 4.0 or fully impossible.

abatishchev
Compiler error you have there!
leppie
@leppie: Thanks, fixed.
abatishchev
Only partly fixed ;P
leppie
@leppie: Yeap, my idea doesn't work :( Maybe optional parameters in .NET 4.0 could help
abatishchev
+2  A: 

If your method is called ParseTable and it fails "Parse" a "Table", then it should throw an exception. The advantage of this is that the exception can give the caller information about why it couldn't parse (html was invalid, unexpected column etc). The problem with returning null is that an unexpected nullreference exception almost never tells you the cause of the problem.

The "right" way to make a method that tries to parse a table, but happily does nothing if no result could be found is:

public bool TryParseTable(HtmlNode table, out DataTable result){
    // your code...
    if(success)
    {
        result = //the table you parsed
        return true;
    }
    else
    {
        result = null;
        return false;
    }
}

Ok, so "result" could be null after calling this method, but at least the caller is more inclined to use an if statement thanks to the return type and method name.

Rob Fonseca-Ensor
@Rob Fonseca-Ensor,What is `out` in parameter for the method ?
Harikrishna
@Harikrishna http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx
Rob Fonseca-Ensor