3

I'm making a method call which takes the results of another four method calls as parameters--but the methods making those calls may or may not be null (sorry if that's a hopelessly unintelligible sentence). Here's the code, if it makes things clearer:

    public void Inform(Room north, Room south, Room east, Room west)
    {
        this.north = north;
        this.south = south;
        this.east = east;
        this.west = west;

        node.Inform(north.GetNode(), south.GetNode(),
                    east.GetNode(), west.GetNode());
    }

Basically, I want to know if there's a quick and easy way to check if an object is null and simply pass 'null' into the method besides conditionals--I'd rather not have to explicitly code for all 16 possible variations of null/not null.

EDIT: In response to confusion, I want to clarify this: most of the time the objects I'm passing into the method won't be null. Usually, Room objects exist for north, south, east, and west, and if the Room exists, the GetNode() method will return the appropriate object. I want to determine if a given Room exists to avoid null reference excpetions upon trying to make a method call.

bionicOnion
  • 1,494
  • 3
  • 16
  • 25
  • 1
    Your question is confusing. If an object is `null`, and you want to pass `null`... why not just pass the object? The `null` value will pass through. Perhaps you could post a short sample of the sixteen-variation code you envision, so we can see exactly what results you want? – Domenic Apr 18 '12 at 01:15
  • I know this is a dupe of another SO question but I can't find it. but what I think you want is something like this: `node.Inform(north == null ? null : north.GetNode(), ...);` – Michael Edenfield Apr 18 '12 at 01:15
  • ok, so not an exact dupe but similar enough to this: http://stackoverflow.com/questions/550374/checking-for-null-before-tostring – Michael Edenfield Apr 18 '12 at 01:17
  • Domenic, most of the time the object won't be null--I expect to have some object which returned to be passed into the method. However, because the object making the call which returns the object I want (i.e. north making a call to GetNode()) may be null, throwing an exception, I want to be able to determine if the Room objects are null. – bionicOnion Apr 18 '12 at 01:21

3 Answers3

9

Create an extension method

static Node GetNodeOrNull(this Room room)
{
  return room == null ? null : room.GetNode();
}
Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
4
 public void Inform(Room north, Room south, Room east, Room west)
    {
        this.north = north;
        this.south = south;
        this.east = east;
        this.west = west;

        node.Inform(GetNode(north), GetNode(south),
                    GetNode(east),GetNode(west));
    } 

    private Node GetNode(Room room)
    {
        return room == null ?  null : room.GetNode();
    }
Thiru kumaran
  • 1,262
  • 1
  • 10
  • 10
3

Ignoring the rest of your code (which I have to :)) - you could start using the Null Pattern. Eg have a NullRoom class and have its GetNode() return something meaningfull. Basically never allow an actual null reference.

Rob Gray
  • 3,186
  • 4
  • 33
  • 34