digitalmars.D.learn - Is it possible to store different subclasses in one array?
- Leonardo (14/14) Apr 12 2020 Is it possible to store different subclasses in one array?
- Adam D. Ruppe (5/7) Apr 12 2020 Did you try
- Leonardo (11/18) Apr 12 2020 Oh, thanks, this works. Now it seems obvious.
- Leonardo (5/27) Apr 12 2020 Replying myself...
- evilrat (4/16) Apr 12 2020 can be simplified as:
- Leonardo (2/20) Apr 13 2020 Even better. Thanks folks!
- Manfred Nowak (7/9) Apr 14 2020 Does the parlor (currently illegal in Dlang)
Is it possible to store different subclasses in one array? public class BaseItem{ public string name = ""; } public class Weapon : BaseItem{ public int damage = 10; } public class Potion : BaseItem{ public int hpRestore = 50; } var GameItems = new List<BaseItem>(); GameItems.Add(new Weapon()); GameItems.Add(new Potion());
Apr 12 2020
On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote:Is it possible to store different subclasses in one array?Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet?
Apr 12 2020
On Monday, 13 April 2020 at 01:47:11 UTC, Adam D. Ruppe wrote:On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote:Oh, thanks, this works. Now it seems obvious. But fitting another question, this case is only representative, if I want to use one method present in only one of these classes like this: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be?Is it possible to store different subclasses in one array?Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet?
Apr 12 2020
On Monday, 13 April 2020 at 04:15:04 UTC, Leonardo wrote:On Monday, 13 April 2020 at 01:47:11 UTC, Adam D. Ruppe wrote:Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack()On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote:Oh, thanks, this works. Now it seems obvious. But fitting another question, this case is only representative, if I want to use one method present in only one of these classes like this: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be?Is it possible to store different subclasses in one array?Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet?
Apr 12 2020
On Monday, 13 April 2020 at 04:21:48 UTC, Leonardo wrote:can be simplified as: if (auto weapon = cast(Weapon) gi) weapon.Attack();foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be?Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack()
Apr 12 2020
On Monday, 13 April 2020 at 05:54:52 UTC, evilrat wrote:On Monday, 13 April 2020 at 04:21:48 UTC, Leonardo wrote:Even better. Thanks folks!can be simplified as: if (auto weapon = cast(Weapon) gi) weapon.Attack();foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be?Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack()
Apr 13 2020
On Monday, 13 April 2020 at 05:54:52 UTC, evilrat wrote:if (auto weapon = cast(Weapon) gi) weapon.Attack();Does the parlor (currently illegal in Dlang) if (Weapon w := gi) w.Attack(); look nicer or even (currently legal): if (Weapon w ._= gi) w.Attack();
Apr 14 2020