-1

So, these are two questions on the same topic.

I was looking for ways to make my accessing of data from types more clean. As an example, let's say I'm making some sort of Health class, and I write some fields for it.

public class Character {
    public Health Health;
}
public class Health {
    public short Current;
    public short Maximum;
}

Now this works, but I'm looking for a way that I could avoid using Health.Current when I want to get the current health value. Something like this:

public class Health {
    private short Current;
    public short Maximum;

    public short this {
        get { return Current; }
        set { Current= value; } 
    }
}

I had expected this to work, but unfortunately it doesn't. Is there any way to accomplish this?

If not, then is there a way to accomplish this instead:

public class Character {
    public short Health;
    public short Health.Maximum;
}

It would have a similar intended effect.

EDIT: Also, I am aware that this is unnecessary, but its something I'd like nonetheless. (Sorry for small mistake in the question, fixed it)

Norse
  • 121
  • 10
  • If you use properties instead of public fields (and the consensus is that [you should](https://stackoverflow.com/questions/1216958/difference-between-automatic-properties-and-public-field-in-c-sharp-3-0)), then your character class can contain `public short MaximumHealth => Health.Maximum;` - is that what you're after? – stuartd Jul 15 '19 at 16:26
  • Not quite, what I'm after is a way to make it so that calling just `Health` will return `Health.Value`, without losing the `Health.Maximum` field, the 3rd code block demonstrates what my final goal is best. (I do favor properties, I just used fields for the sake of example) Sorry, I made a mistake in my code, I fixed it now, question should be clearer. – Norse Jul 15 '19 at 16:34
  • 1
    Well, you _could_ add an implicit operator from the health class to a short which returns the current value - `public static implicit operator short(Health health) { return health.Current ; }` – stuartd Jul 15 '19 at 16:39
  • That has exactly the functionality I was looking for, you can post it as an answer. Though is there any reason the 'could' is in italics, is there any loss other than the ability to directly access the instance of the `Health` class itself? – Norse Jul 15 '19 at 16:44
  • The 'could' was just an indication that it was an option you might want to take, or not. – stuartd Jul 15 '19 at 16:47
  • 1
    You *could* do this, but it would be confusing to most readers of your code. If `character.Health` should return the current health, then make that a property that returns the current health, and make `MaximumHealth` another property. Conversion operators are there for convenience when you're not changing the meaning of something. e.g. numeric conversions that don't lose precision. – Asik Jul 15 '19 at 16:53

1 Answers1

1

You could add an implicit operator from the health class to a short which returns the current value:

public class Health
{
    public static implicit operator short(Health health)
    { 
        return health.Current;
    }

    public short Current;
    public short Maximum;
}

The use it like this:

Health h = new Health { Current = 20, Maximum = 100};
short current = h;
Console.WriteLine(current); // 20
stuartd
  • 70,509
  • 14
  • 132
  • 163