Reputation: 147
I am calling a webservice that returns an array of one of four custom classes. All classes have the same inner contents - one string named Description, and another string named Value. I am trying to write a single method that can accept any of the four classes, and put its contents into a dropdown list's datasource.
Is there a way to convert from an unknown composite class to a specified class with the same contents? Or strip the contents out?
Or will I have to write four identical functions with different datatypes?
edit: added code
myDropDown.DataSource = CreateDataSource(myWebServiceResponse.Items);
myDropDown.DataTextField = "DescriptionField";
myDropDown.DataValueField = "ValueField";
// Bind the data to the control.
myDropDown.DataBind();
...
public ICollection CreateDataSource(MasterData[] colData)
{
// Create a table to store data for the DropDownList control.
DataTable dt = new DataTable();
// Define the columns of the table.
dt.Columns.Add(new DataColumn("DescriptionField", typeof(String)));
dt.Columns.Add(new DataColumn("ValueField", typeof(String)));
// Populate the table
foreach (sapMasterData objItem in colData)
{
dt.Rows.Add(CreateRow(objItem, dt));
}
// Create a DataView from the DataTable to act as the data source
// for the DropDownList control.
DataView dv = new DataView(dt);
return dv;
}
DataRow CreateRow(MasterData objDataItem, DataTable dt)
{
// Create a DataRow using the DataTable defined in the
// CreateDataSource method.
DataRow dr = dt.NewRow();
dr[0] = objDataItem.Description;
dr[1] = objDataItem.Value;
return dr;
}
public class MasterData
{
public string Value;
public string Description;
}
Upvotes: 0
Views: 170
Reputation: 23300
A wrapper would do, you have two approaches:
public class WSData
{
public string Value;
public string Description;
// First approach: single ctor with dynamic parameter
public WSData(dynamic source)
{
this.Value = source.Value;
this.Description = source.Description;
}
// ----- or --------
// Second approach: one ctor for each class
public WSData(FirstTypeFromWS source)
{
this.Value = source.Value;
this.Description = source.Description;
}
public WSData(SecondTypeFromWS source)
{
this.Value = source.Value;
this.Description = source.Description;
}
}
usage is the same:
WSData yourData = new WSData(data_retrieved_from_service);
// now, bind the WSData object: you have abstracted yourself from
// the service and as a bonus your code can be attached elsewhere more easily
Upvotes: 1
Reputation: 3323
You could define an interface that it is implemented by all four classes (e.g. IDescriptionValue) and write a method that accepts a parameter of that type, for instance:
public interface IDescriptionValue
{
public string Description {get;set;}
public string Value {get;set;}
}
public class CustomClass1 : IDescriptionValue{
public string Description {get;set;}
public string Value {get;set;}
}
//snip...
public class CustomClass4 : IDescriptionValue{
public string Description {get;set;}
public string Value {get;set;}
}
//accepts parameters of type IDescriptionValue
public void setDropdownData(IDescriptionValue inputData){
// your code here
}
Upvotes: 0
Reputation: 1038810
Actually the DropDownList
control requires you an IEnumerable
as DataSource and then you could specify the DataTextField
and DataValueField
:
dropDownList.DataSource = some_Array_You_Retrieved_From_Your_Web_Service;
dropDownList.DataValueField = "Value";
dropDownList.DataTextField = "Description";
dropDownList.DataBind();
As you can see it doesn't really matter the actual type of the array, as long as it has Value
and Description
properties to bind it to.
Upvotes: 2