haezeban

haezeban

  • NA
  • 6
  • 0

Generics

Feb 12 2010 9:23 AM

Hi ,
 
Quite new to generics, I have a lot of methods with al the same code only the used classobject is different.But I do not know how to make this generic (it gives me errors) :
private
void btn_Click(object sender, EventArgs e)
{
ArrayList ar = new ArrayList();
ar.Add(
"");
this.txtUitleg.Lines = (string[])ar.ToArray(typeof(string));
typeDataSource =
typeof(classname1);
typeItems =
classname1.Items.GetType();
codeTabel =
"classname1";
dataGridView2.DataSource =
classname1.Items;
lblTitel.Text = codeTabel;
}
 
This is the code I have for about 10 different button_clicks events. Only the classname1 is different. So I think this is a good piece of code to use generics.
 
What I tried :
 
private void btn_Click(object sender, EventArgs e)
{
   GenericsButton_Click();
}

private void GenericsButton_Click<T>()
{
ArrayList ar = new ArrayList();
ar.Add(
"");
this.txtUitleg.Lines = (string[])ar.ToArray(typeof(string));
typeDataSource =
typeof(T);
typeItems =
T.Items.GetType();
codeTabel =
T.ToString();
dataGridView2.DataSource =
T.Items;
lblTitel.Text = codeTabel;
}

 
But this is not correct. Is it possible to point me out ?
Thanks
 

Answers (4)