Generics, a bed time story...

by Mikael Henriksson 27. January 2009 18:59

Good evening,

Since I was totally struggeling with this I thought I'd better offer my expertice in this topic. I'll give a short story about the problems I faced and share the code that solved the problem at the very end.

The problem originated with not being able to bind a list of simple structs to a DataGridView in a WinForm application. Me and my colleague where about to actually pull our hairs of our heads one by one. We examined the return list and sure it contained items. It was all there but nothing would show up in the DataGridView.

Later it turns out none of the items were added as new meaning the DataGridView can't hmm wrong word maybe but index it. I could do nothing about the Struct we used for generating the data since this is done by an ocx connection so I asked around and was told to create a holder class that took the list of structs in an recreated it as a bindable list. Simple huh?

Now to the real stupid part. The perfectionist in me completely took over! I really wish I didn't have this obsession with reusable code. I really wanted to reuse the method for converting the non-bindable list to a bindable list :)

When I created the general conversion method I added two types let's call them HolderType and OriginType. First I got the silly message:

-"does not have a new constraint"
which I had to look up. Stupid thing was really messing with me so I added:

where HolderType : new()

Should do the trick right? Well no! No it said I could not use parameter when adding new. Some more reading and 3 hours later I finally found the solution!

First lets take a simple struct with an id and a value. There is actually 600 structs * 3.

 

public struct SimpleStruct
{
    public int Id;
    public string Value;
}

 

Then I need to wrap this struct in a class:

 

public class StructHolder
{
    private SimpleStruct _simpleStruct;

    public StructHolder(SimpleStruct simpleStruct)
    {
        _simpleStruct = simpleStruct;
    }

    public StructHolder()
    {
        _simpleStruct = new SimpleStruct();
    }

    public SimpleStruct SimpleStruct
    {
        get { return _simpleStruct; }
    }
    public int Id
    {
        get { return _simpleStruct.Id; }
    }

    public string Value
    {
        get { return _simpleStruct.Value; }
    }
}

 

 

And then I need a method to work on a collection of a struct and convert it to a collection of class :)

// Method to convert a list of structs to a bindable list
private IList<HolderType> GenerateUsableList<HolderType, OriginType>(List<OriginType> originlist) 
	where HolderType : new()
{    // Create the usable list    
	var holderlist = new List<HolderType>();

	originlist.ForEach(origintype => 
		holderlist.Add((HolderType) 
		Activator.CreateInstance(typeof (HolderType), origintype)));    
	
	return holderlist;
}

Tags:

C#

blog comments powered by Disqus

About the author

Life architect specialized in programming