5 Replies - 10228 Views - Last Post: 18 June 2009 - 09:49 AM Rate Topic: -----

#1 SoftwareTester   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-August 08

how to create a derived class from List<T>

Posted 18 June 2009 - 06:47 AM

I have several classes all containg a List<T> object .
In each class I have the 'same' Add / Delete / Search functions.
Of course i want to use a class derived from List<T> and implement those Add / Delete / Search functions just once.
Something like
public class ListRef<T> : List<T>
{
   private ListRef<T> MyList = null;

   ListRef<T>
   {
	   MyList<T> = new ListRef<T>();
   }

   public void Add(T Object)
   {
	   MyList.Add(Object);
   }
} 

public class MyClass
{
	 private ListRef<Class_A> ListOfObjects_A = null;
	MyClass()
   {
	   ListOfObjects_A = new ListRef(Class_A>();
   }
}



but I doubt if the code above is correct.

How can I achieve what I want (I hope it's clear what I want)?

Is This A Good Question/Topic? 0
  • +

Replies To: how to create a derived class from List<T>

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: how to create a derived class from List<T>

Posted 18 June 2009 - 07:43 AM

No so clear.

public class ListRef<T> : List<T> {
	// why do you need this?  
	// It's private, who else gets to see it?
	//private ListRef<T> MyList = null;

	// public, you want others to be able to create an instance, right?
	public ListRef { }
	
	// this is redundant
	// public void Add(T item) { this.Add(item); }
} 




Perhaps you're thinking this:

public class ListA : List<Class_A> { }

public class MyClass {
	private ListA listA = null;
	
	public MyClass() {
		listA = new ListA();
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 SoftwareTester   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-August 08

Re: how to create a derived class from List<T>

Posted 18 June 2009 - 08:01 AM

I'll try to clarify.

Quote

public class ListA : List<Class_A> { }public class MyClass { private ListA listA = null; public MyClass() { listA = new ListA(); }}


If I'm not mistaken class ListA handles a List of objects Class_A
but I also want it to handle objects of Class_B and Class_C etc
so it should handle
public class ListA : List<Class_A>
public class ListA : List<Class_B>
public class ListA : List<Class_C>

so the parameter should be variable, that's why I used the <T>
public class ListRef : List<T>

but how should I create the list in the constructor of each class that uses such a list?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: how to create a derived class from List<T>

Posted 18 June 2009 - 08:42 AM

I really depends on what you're trying to do. The only real reason to extent a class is if you intend to add functionality.

e.g.

class Point {
	public int x,y;
}

class ListPoint : List<Point> {
	public void Add(int x, int y) {
		Point pt = new Point();
		pt.x = x;
		pt.y = y;
		this.Add(pt);
	}
	
	public Point GetClosest(Point pt) {
		//...
	}
}



If you're just using the basic thing, you don't need to do anything:
List<Class_A> listA = new List<Class_A>();
List<Class_B> listB = new List<Class_B>();



If classes have a common parent, you can lump them together.
abstract class ClassBase { }
class ClassA : ClassBase { }
class ClassB : ClassBase { }

List<ClassBase> listAB = new List<ClassBase>();


Was This Post Helpful? 0
  • +
  • -

#5 SoftwareTester   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-August 08

Re: how to create a derived class from List<T>

Posted 18 June 2009 - 09:28 AM

My classes will hold lists of objects of DIFFERENT classes. But some common operations like Add, Delete, Search have to be performed on all those objects. Of course i want to add functionality, but if I would put all the code in these postings it would just become confusing.

So my ListRef class must be able to store objects of DIFFERENT classes into a list . If so I can write Add, Delete, Search 9etc) into the ListRef class instead of writing (almost) the same code inside each of the different classes.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: how to create a derived class from List<T>

Posted 18 June 2009 - 09:49 AM

The point of generics is type safety. However, if you're just looking to store anything and are unwilling to set criteria:
List<object> listAny = new List<object>;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1