pass a class as argument

P

powerranger

The code below gets data from my webservice and load the data into the
array called myarray using a class called DataClass. I would like to
use the same process (LoaData) to bind differrent comboboxes by using
different classes. How can I modify the LoadData so that I can pass in
the class -- private void LoadData(System.Windows.Forms.ComboBox cmbo,
??). Any sample of code would be very helpfull. Thanks.


private void LoadData(System.Windows.Forms.ComboBox cmbo)
{
WebService.GetData mydata = new WS.GetData();
DataSet Ds = mydata.GetInfo();
foreach (DataRow Dr in Ds.Tables["StateList"].Rows)
{
MyArray.Add(new DataClass(Convert.ToInt32(Dr["ID"].ToString()),
Dr["Desc"].ToString()));
}
cmbo.DisplayMember = "Desc";
cmbo.ValueMember = "Id";
cmbo.DataSource = MyArray;
 
T

TheGrudge

Instead of passing a user created class, just pass a DataSet object to the
method. That way you can load it with whatever.

As you can see I use the index of zero to access the table from the dataset.
I'm assuming that there is only one table. If not, add the table name as
another parameter to the method

private void LoadData(System.Windows.Forms.ComboBox cmbo,DataSet ds,string
valueMember,string displayMember)
{
foreach (DataRow Dr in ds.Tables[0].Rows)
{
MyArray.Add(new DataClass(Convert.ToInt32(Dr[valueMember].ToString()),
Dr[displayMember].ToString()));
}
cmbo.DisplayMember = displayMember;
cmbo.ValueMember = valueMember;
cmbo.DataSource = MyArray;
cmbo.DataBind();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top