Reputation: 13549
In my program there is a lot of classes that have same name variables (because they are fetched from a database and the variable name needs to be an exact match to the name in the database).
While subclassing is a great option, its also excessive and does not handle the case I am after. Specifically, if my base class held say 100 variables, all my subclasses would require any random combination of them, requiring almost as many base classes as subclasses. Hence the ideal route would be some sort of variable name aliasing.
I was wondering if there is a way to have a Variable-Name-Alias that all my classes could share.
So instead of having
//an armor class
public class Armor
{
public string name{ get; set; }
}
//a weapon class
public class Weapon
{
public string name{ get; set; }
}
I could have something like
static class Aliases
{
define name as NAME<String> //or 'something' like this
}
And then have
//an armor class
public class Armor
{
public string NAME{ get; set; }
}
//a weapon class
public class Weapon
{
public string NAME{ get; set; }
}
Then just in case we need to change the code or the database, I dont have to go through an update EVERY classes variable that was associated. I just rename or remove the variable name alias. Is this possible (or am I dreaming)?
Upvotes: 3
Views: 3097
Reputation: 66439
You could create an abstract base-class, with the similar name in it. At least then there's only one place to change it.
public abstract class Item
{
public string NAME { get; set; }
}
public class Armor : Item
{
// Other armor-specific stuff
}
public class Weapon : Item
{
// Other weapon-specific stuff
}
Upvotes: 2