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)
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