Generics question?  
Author Message
DodgySwampy





PostPosted: .NET Base Class Library, Generics question? Top

Hi folks,

Can anyone tell me how I can call my GetOutputXml function from within my generic list class

My list class:

public class List<T> : CollectionBase

{

public List() { }

public T this[int index]

{

get { return (T)List[index]; }

set { List[index] = value; }

}

public int Add(T value)

{

return List.Add(value);

}

public string GetOutputXml()

{

string outputXml = null;

for (int i = 0; i < List.Count; i++ )

{

T t = (T)ListIdea;

// THIS DOESN'T WORK!

// HOWEVER, ALL MY CLASSES INHERIT FROM A BASE CLASS THAT HAS THIS FUNCTION.

outputXml += t.GetOutputXml;

return outputXml;

}

}

}

I know I'm probably doing it wrong, so any help would be much appreciated.



.NET Development30  
 
 
Andrej Tozon





PostPosted: .NET Base Class Library, Generics question? Top

Hi,

try attaching a constraint to your generic class:

public class List<T> : CollectionBase
   
where T: MyBaseClass
{
    ...
}

... where MyBaseClass is of course your base class, which implements GetOutputXml() method.

SideNote: you could use the StringBuilder class for concatenating your outputXml string to increase the performance...

Andrej



 
 
Swampy





PostPosted: .NET Base Class Library, Generics question? Top

Hi Andrej,

Thanks very much for that. Youre solution worked a treat!

Swampy