My question has sort of already been asked here: How to build a group of constant variables in c# But I feel as though it is not quite asking the same thing. I will try to explain, please hear me out.
Suppose for a moment that you had to represent attributes for a player, such as Strength, Agility, Stamina, etc. But at the same time you also wanted to list other attributes which were not directly related to the same group. Aside from Primary Attributes you could have Probabilistic Attributes, such as Accuracy, Evasion, and Critical Strike. As well as Logistical Attributes, like: Movement, Initiative, and Jump. Naturally, these Attributes serve different purposes, and thus, should be grouped seperately(Or at least in my mind they should). I could implement this using multiple classes:
namespace Example
{
public class PrimaryAttributes
{
public int Strength { get; set; }
public int Agility { get; set; }
public int Stamina { get; set; }
public int Intellect { get; set; }
public int Willpower { get; set; }
public int Spirit { get; set; }
public PrimaryAttributes()
{
// Do stuff here
}
// Do more stuff here
}
}
Continued...
namespace Example
{
public class ProbabilisticAttributes
{
public int Accuracy { get; set; }
public int Evasion { get; set; }
public int CriticalStrike { get; set; }
public ProbabilisticAttributes()
{
// Do stuff here
}
// Do more stuff here
}
}
Continued...
namespace Example
{
public class LogisticalAttributes
{
public int Movement { get; set; }
public int Initiative { get; set; }
public int Jump { get; set; }
public LogisticalAttributes()
{
// Do stuff here
}
// Do more stuff here
}
}
And then host them all in a single class, like this:
namespace Example
{
public class Statistics
{
public PrimaryAttributes PrimaryAttributes { get; set; }
public ProbabilisticAttributes ProbabilisticAttributes { get; set; }
public LogisticalAttributes LogisticalAttributes { get; set; }
public LogisticalAttributes()
{
PrimaryAttributes = new PrimaryAttributes();
ProbabilisticAttributes = new ProbabilisticAttributes();
LogisticalAttributes = new LogisticalAttributes();
}
// Do more stuff here
}
}
That would achieve the effect of being able to call something along the lines of "stats.PrimaryAttributes.Strength", which is clean and organized. However, I would prefer to not do this. I would like to host all the variables within the same class without having to use other classes. So what's the catch? I still want to be able to organize them behind an extended scope, or I guess what could be called a namespace; a sort of divider, if you will. I don't think anything like this exists within the C# language...or any language for that matter. But I want to know how close I can get to replicating this behavior. Here is an example of what I am looking for (not real C#, just an example).
namespace Example
{
public class Attributes
{
group PrimaryAttributes // Imaginary "group" keyword.
{
int Strength { get; set; }
int Agility { get; set; }
int Stamina { get; set; }
int Intellect { get; set; }
int Wisdom { get; set; }
int Spirit { get; set; }
}
group ProbabilisticAttributes // Imaginary "group" keyword.
{
int Evasion { get; set; }
int Accuracy { get; set; }
int CriticalStrike { get; set; }
}
group LogisticalAttributes // Imaginary "group" keyword.
{
int Movement { get; set; }
int Initiative { get; set; }
int Jump { get; set; }
}
public Attributes()
{
// Don't have to declare or initialize anything.
}
public int ReturnSomethingAmazing()
{
return this.PrimaryAttributes.Strength * this.PrimaryAttributes.Agility; // Notice the group accessor usage.
}
// Do more stuff here
}
}
I hope you can see what I am talking about now. I want to be able to "scope" variables, but without the use of separate classes. I have also taken the liberty of thinking about how this could be parsed at compile time. Essentially, the group scopes would just be inlined, so myClassInstance.PrimaryAttributes.Strength
, would be minimalized to myClassInstance.Strength
. The scope doesn't actually exist, it's only there for the programmer to better facilitate organization, without expending more objects in memory at runtime.
To end this, I wanted to summarize two questions.
Is this in any way possible using current C#?
and
Do you think this would be a good suggestion to recommend to Microsoft for an addition to the C# programming language?