Collection and abstract classes
-
03 Januari 2012 10:43
First of all, move this if this doesn't belong here.
Anyway, I'm creating a map editor for a game. I have now created the map editor itself and it's time to move on to place monsters on the map. So I create an abstract class Monster (no monster can be just a monster, every monster has to have a race). Then I create a class Dragon. Dragon inherits properties like strength and location from monster, which it then overrides. So far pretty clear.
But how does collections play into this? I mean, I want to be able to go thru all monsters on the map (for example, to be able to write the database file for the game). So I need a class Monsters? Dragon inherits from Monster, but how I'll draw the lines to put in the collection there as well? Does the collection come between the class and the abstract class? Or where does it go?
- Dipindahkan oleh Esther FanMicrosoft Employee 23 April 2012 1:08 (From:Visual Studio Class Designer)
Semua Balasan
-
05 Januari 2012 5:30
Hello Jaymond,
Thank you posting!
This is a quick note to let you know that I am performing research on this issue and will get back to you as soon as possible. I appreciate your patience.
Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us
-
05 Januari 2012 7:13Moderator
Hi Jaymond,
Welcome to MSDN Forums!
For the collection, I would recommend you consider the .NET generic list classes, like List<T>. List<Monster> list = new List<Monster>(); Then we can still add Dragon objects into the list, and other classes inheriting Monster class as well.
Or if you want another class to represent the Monster list since you need to store some data in the this class, you may consider such two methods:
1) Inherit the List<Monster> class directly.
class MonsterList : List<Monster> { // declare other properties or fields }
2) Declare the List<Monster> as a property inside the class
class MonsterList2 { public List<Monster> List { get; set; } // declare other properties or fields }
Please feel free to let me know if you have any questions. Besides, the question does not belong to this forum queue. I believe Visual C# General forum or VB.NET General forum are more appropriate according to the language you are using.
Have a nice day!
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
- Diedit oleh Michael Sun [MSFT]Microsoft Employee, Moderator 05 Januari 2012 7:14
- Ditandai sebagai Jawaban oleh Michael Sun [MSFT]Microsoft Employee, Moderator 18 Januari 2012 7:55