Including an additional enum property into an object?
-
How can I assign an additional "enum" property to a pre-existing object? I'm building a Trophy system for my website where user's can earn trophies for various achievements. I currently have a "Trophy" object which was created for me automatically in Linq when I created my dbml file. Nice and simple so far :) Now however, I want to include an additional property to my "Trophy" object of type enum called "TrophyLevel". For several, long winded reasons, I don't want to include TrophyLevel as a part of the actual "Trophy" object. So, I've tried setting it up in a partial class like the following:
public partial class Trophy
{
public enum TrophyLevel
{
Bronze = 1,
Silver = 2,
Gold = 3,
Platinum = 4
}
}Is this the correct way I should be setting this up, and if so, how do I access it? So, basically, I want to be able to have code like the following: Trophy.TrophyLevel = 1; OR String level = Trophy.TrophyLevel; Am I going about this correctly? Thanks
-
How can I assign an additional "enum" property to a pre-existing object? I'm building a Trophy system for my website where user's can earn trophies for various achievements. I currently have a "Trophy" object which was created for me automatically in Linq when I created my dbml file. Nice and simple so far :) Now however, I want to include an additional property to my "Trophy" object of type enum called "TrophyLevel". For several, long winded reasons, I don't want to include TrophyLevel as a part of the actual "Trophy" object. So, I've tried setting it up in a partial class like the following:
public partial class Trophy
{
public enum TrophyLevel
{
Bronze = 1,
Silver = 2,
Gold = 3,
Platinum = 4
}
}Is this the correct way I should be setting this up, and if so, how do I access it? So, basically, I want to be able to have code like the following: Trophy.TrophyLevel = 1; OR String level = Trophy.TrophyLevel; Am I going about this correctly? Thanks
Yes, this is correct. EF generated a partial class for Trophy which you would extend by adding another partial class definition in the same namespace. http://msdn.microsoft.com/en-us/library/wa80x488(v=VS.100).aspx[^]
No comment
-
How can I assign an additional "enum" property to a pre-existing object? I'm building a Trophy system for my website where user's can earn trophies for various achievements. I currently have a "Trophy" object which was created for me automatically in Linq when I created my dbml file. Nice and simple so far :) Now however, I want to include an additional property to my "Trophy" object of type enum called "TrophyLevel". For several, long winded reasons, I don't want to include TrophyLevel as a part of the actual "Trophy" object. So, I've tried setting it up in a partial class like the following:
public partial class Trophy
{
public enum TrophyLevel
{
Bronze = 1,
Silver = 2,
Gold = 3,
Platinum = 4
}
}Is this the correct way I should be setting this up, and if so, how do I access it? So, basically, I want to be able to have code like the following: Trophy.TrophyLevel = 1; OR String level = Trophy.TrophyLevel; Am I going about this correctly? Thanks
Yes, you are certainly creating a valid extension to the Trophy class via using the 'partial keyword, and I assume you realize that both the original and added classes should/must be declared with the partial keyword:
public partial class Trophy
If I assume, based on what you present here, that 'Trophy is not a static class, then you are going to be creating instances of Trophy: you'll be using the instances like this:
Trophy tp1 = new Trophy();
Trophy tp2 = new Trophy();tp1.theTrophyLevel = Trophy.TrophyLevel.Platinum;
tp2.theTrophyLevel = Trophy.TrophyLevel.Silver;String level = tp1.theTrophyLevel.ToString();
"Anyone who shows me my 'blind spots' gives me the gift of sight." ... a thought from the shallows of the deeply shallow mind of ... Bill