Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Abstract Generic class?
Message
From
12/10/2009 16:02:24
 
 
General information
Forum:
ASP.NET
Category:
Class design
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01429024
Message ID:
01429043
Views:
64
What I usually do when I get stuck between a rock and a hard place, as this dilemma seems to be, is to kludge it a bit and do this:
public virtual void LoadItems()
{
    MessageBox.Show("Developer Error! The LoadItems() method must be overridden!");
}
Not pretty, but it works.

~~Bonnie




>Hi,
>
>I have a generic class - in a cutdown form so:
public class BaseTVItemViewModel<U> : ViewModelBase
>     where U : EntityObject
>    {
>        Interfaces.IsTreeModel<U> _treeModel;
>        List<BaseTVItemViewModel<U>> _children;
>        BaseTVItemViewModel<U> _parent;
>
>        public BaseTVItemViewModel(Interfaces.IsTreeModel<U> tm)
>        {
>            _treeModel = tm;
>        }
>
>       public BaseTVItemViewModel<U> Parent
>        {
>            get
>            {
>                if (_parent == null)
>                {
>                    _parent = new BaseTVItemViewModel<U>((Interfaces.IsTreeModel<U>)_treeModel);
>                }
>                return _parent;
>            }
>            set { _parent = value; }
>        }
>
>        public List<BaseTVItemViewModel<U>> Children
>        {
>            get
>            {
>                if (_children == null)
>                {
>                    _children = new List<BaseTVItemViewModel<U>>();
>
>                    foreach (U u in _treeModel.GenericChildren)
>                    {
>                        _children.Add(new BaseTVItemViewModel<U>((Interfaces.IsTreeModel<U>)u));
>                    }
>                }
>                return _children;
>            }
>
>        }
>
>        public virtual void LoadItems()
>        {
>        }
>    }
>
I wanted to make the LoadItems() method abstract but I can't make the class abstract because constructing a new instance of an abstract class (as in the Children property) isn't allowed :-{
>
>Is there a better way of designing this?
>TIA,
>Viv
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform