I have an custom class that I use for populating an ArrayList that I then use as the DataSource for a listbox in ASP.Net.
Problem is it was written in
VB but needs to be C#. I have completed the conversion except for one statement that does not work as it did in
VB.
Problem statement is when trying to pull the SelectedItem.
VB: (Works Great!)
strCode = CType(cboLocation.SelectedItem,clsListElements).Lo cType
C#: (Fails with an error)
strCode = ((clsListElements)System.Convert.ChangeType(cboLoc ation.SelectedItem, typeof(clsListElements))).LocType;
ERROR: 'System.InvalidCastException' {"Object must implement IConvertible."}
My custom class C# (Partial Code):
public class clsListElements
{
private string strDescription;
private string strID;
private string strLocType;
public clsListElements(string strPassedDescription, string strPassedID, string strPassedLocType)
{
this.strDescription = strPassedDescription;
this.strID = strPassedID;
this.strLocType = strPassedLocType.Trim();
}
public string LocType
{
get
{
return this.strLocType;
}
}
}
Code to load arraylist and to set listbox datasource to that arraylist:
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
if (myReader["LocationType"].ToString() == CommonRoutine.C_LOCATIONTYPE_BOTH || myReader["LocationType"].ToString()
== CommonRoutine.C_LOCATIONTYPE_TEST)
{
aryList.Capacity++;
aryList.Add(new clsListElements((string)myReader["LocationName"], myReader["LocationID"].ToString(), myReader{"LocationType"].ToString()));
}
}
cboLocation.DataSource = aryList;
cboLocation.DataTextField = "Description";
cboLocation.DataValueField = "IDNum";
cboLocation.DataBind();
myReader.Close();