Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Abstract Generic class?
Message
From
13/10/2009 00:16:44
 
 
General information
Forum:
ASP.NET
Category:
Class design
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01429024
Message ID:
01429087
Views:
63
>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

Viv,

(1) As you have found out: If a class is abstract, then there are parts missing and it cannot be instantiated

(2) Once subclassed, what will become of the new BaseTVItemViewModel() ?
The classname won't be correct anymore - or doesn't that matter ?


Solutions
(1) Like Bonnie says

(2) Add another abstract method which returns a new instance
Since the method is abstract - I have discovered it cannot be static - Create() is usually static

You now have two abstract methods: LoadItems() and Create()
	abstract class AbstractBaseClass
	{
		protected AbstractBaseClass Child = null;
		//______________________________________________________________________
		public abstract AbstractBaseClass Create();
		//______________________________________________________________________
		public abstract void LoadItems();
		//______________________________________________________________________
		public void xx()
		{
			Child = Create();
		}
		//______________________________________________________________________

	}

	class DerivedClass : AbstractBaseClass
	{
		//______________________________________________________________________
		public override AbstractBaseClass Create()
		{
			return new DerivedClass();  // or AbstractBaseClass()
		}
		//______________________________________________________________________
		public override void LoadItems()
		{
			Child = Create(); // 
		}

	}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform