-1

A Device can have a ConnectedBattery sometimes its value is null, I try to display some details of the Device like:

  allDetails.Add("Conected to " + ConnectedBattery.Name);

If ConnectedBattery is null, I want to return ConnectedBattery.Name as "none". I tried Doing this in GameObjects (Which both Battery and Device inherite from)

public string Name
{
    get
    {
        if (this == null)
        {
            return "none";
        }
        return _name;
    }
    set { _name = value; }
}

is something like this even possible? or will is just have to do a null check in the Details

Ryan
  • 103
  • 5
  • 1
    No. If the object is null, there is no `this`. You have to do something like `string batteryName = connectedBattery?.Name ?? "none";` – Rufus L May 16 '17 at 02:40
  • `ConnectedBattery?.Name ?? "none"` - [null conditional operator](https://learn.microsoft.com/en-us/dotnet/articles/csharp/language-reference/operators/null-conditional-operators) and [null coalescing operator](https://learn.microsoft.com/en-us/dotnet/articles/csharp/language-reference/operators/null-conditional-operator) - It will return "none" in the case that ConnectedBattery OR Name is null. – ProgrammingLlama May 16 '17 at 02:41

2 Answers2

0

Actually, in your specific example where you have a private backing field, you CAN do something similar to that. If the entire object is null, then there is no this, but if only the backing field _name is null, then you could do this:

public string Name
{
    get
    {
        if (_name == null)
        {
            return "none";
        }
        return _name;
    }
    set { _name = value; }
}

Otherwise, from the calling code, you will have to check if the object itself is null before trying to access any properties of it (using an example without null coalescing):

string batteryName = (connectedBattery == null) ? "none" : ConnectedBattery.Name;
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

There are two ways you can do this:

1) Use a null conditional operator .?, and a null coalescing operator ??:

allDetails.Add("Connected to " + ConnectedBattery?.Name ?? "none");

This will output "none" when ConnectedBattery or Name is null.

2) Use a static method:

public static string GetBatteryName(Battery battery)
{
    return battery?.Name ?? "none";
}

allDetails.Add("Connected to " + Battery.GetBatteryName(ConnectedBattery));

3) Use an extension method:

public static class BatteryExtensions
{
    public static string GetBatteryName(this Battery battery)
    {
        return battery?.Name ?? "none";
    }
}

allDetails.Add("Connected to " + ConnectedBattery.GetBatteryName());

2 and 3 will achieve the same as 1), but you can use them from multiple places without always writing the null coalescing code.

Personally, I'd recommend 1) or 2) since it's more explicit about what you're doing. It isn't always obvious to people that extension methods can be called even when the object they're on is null, so people might get confused by your code if you go with 3).

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Why extension method? – CodingYoshi May 16 '17 at 02:54
  • @CodingYoshi Well, he could just use a plain static method, but an extension method would allow him to just call .GetBatteryName() without worrying if ConnectedBattery is null or not, as opposed to passing ConnectedBattery to the static method directly. Why not? – ProgrammingLlama May 16 '17 at 02:56
  • I'm not the OP, but how would you code it to recognize the three states; no battery, no battery.name, & battery,name? – Mad Myche May 16 '17 at 03:06
  • In the static method and extension method, you can simply write code to suit your needs. I've created an [example](https://dotnetfiddle.net/C80KyI) for you. – ProgrammingLlama May 16 '17 at 04:11
  • @MadMyche Sorry, I forgot to tag you by name :) See above comment. – ProgrammingLlama May 16 '17 at 04:32
  • @john Thank you, that is what I would I would have done as well; didn't know if there was way to use null conditional/coalescing operators for it. – Mad Myche May 16 '17 at 10:55