Hi All
Complete newby question.
I have some objects I want to generate and make available through a method that can be called to return them in a dictionary collection. There are a few different objects but they share some properties and methods so I thought this would be a good time to use inheritance. But this is all new to me so I think I am designing this incorrectly. There is a table in a database which decides which objects need to be generated and also holds some of their properties.
I have simplified the code to make this posting easier.
Classes for the objects
public class ExampleBaseClass
{
//common properties
public string objectName;
public string prop1;
public string prop2;
public string prop3;
}
public class ExampleInheritClass1 : ExampleBaseClass
{
//addiotnal properties
public string extraprop1;
public string extraprop2;
//More object specific methods and properties go here
}
public class ExampleInheritClass2 : ExampleBaseClass
{
//addiotnal properties
public string another_extraprop1;
public string another_extraprop2;
//More object specific methods and properties go here
}
Class to retrieve them
public class myClassToGetObjects
{
public Dictionary<string, ExampleBaseClass> dictmyObjects = new Dictionary<string, ExampleBaseClass>();
private DataTable dataTableOfObjects = new DataTable();
public myClassTogetObjects(string objectName)
{
getObjectsDataFromDatabase();
foreach (DataRow row in dataTableOfObjects.Rows)
{
if (row["objectName"].ToString() == objectName)
{
populateObjectDict(row);
}
}
}
private void getObjectDataFromDatabase()
{
SqlConnection myConnection = new SqlConnection("server=test;" + "Trusted_Connection=yes;" + "database=testDB; " + "connection timeout=30");
SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * MY_TABLE_OF_OBJECTS", myConnection);
myAdapter.Fill(dataTableOfObjects);
myConnection.Close();
myConnection = null;
}
private void populateObjectDict(DataRow objDataRow)
{
ExampleBaseClass MyObject = new ExampleBaseClass();
MyObject.objectName = objDataRow["objectName"].ToString();
MyObject.prop1 = objDataRow["prop1"].ToString();
MyObject.prop2 = objDataRow["prop2"].ToString();
MyObject.prop3 = objDataRow["prop3"].ToString();
switch (objDataRow["ObjectType"].ToString())
{
case "Example1":
//set object specific properties
break;
case "Example2":
//set object specific properties
break;
dictmyObjects.Add(MyObject.objectName, MyObject);
}
}
}
I am stuck with the last method, populating the dictionary with my objects.
I want to have one MyObject which is set to the correct type based on the ObjectType from the database and then add this to the dictionary as I loop through. But I can't see to do this as MyObject will only ever have the properties of the base class. How do I turn it into one of the derived classes as I loop through? Or am I going about this in the wrong way?
Complete newby question.
I have some objects I want to generate and make available through a method that can be called to return them in a dictionary collection. There are a few different objects but they share some properties and methods so I thought this would be a good time to use inheritance. But this is all new to me so I think I am designing this incorrectly. There is a table in a database which decides which objects need to be generated and also holds some of their properties.
I have simplified the code to make this posting easier.
Classes for the objects
public class ExampleBaseClass
{
//common properties
public string objectName;
public string prop1;
public string prop2;
public string prop3;
}
public class ExampleInheritClass1 : ExampleBaseClass
{
//addiotnal properties
public string extraprop1;
public string extraprop2;
//More object specific methods and properties go here
}
public class ExampleInheritClass2 : ExampleBaseClass
{
//addiotnal properties
public string another_extraprop1;
public string another_extraprop2;
//More object specific methods and properties go here
}
Class to retrieve them
public class myClassToGetObjects
{
public Dictionary<string, ExampleBaseClass> dictmyObjects = new Dictionary<string, ExampleBaseClass>();
private DataTable dataTableOfObjects = new DataTable();
public myClassTogetObjects(string objectName)
{
getObjectsDataFromDatabase();
foreach (DataRow row in dataTableOfObjects.Rows)
{
if (row["objectName"].ToString() == objectName)
{
populateObjectDict(row);
}
}
}
private void getObjectDataFromDatabase()
{
SqlConnection myConnection = new SqlConnection("server=test;" + "Trusted_Connection=yes;" + "database=testDB; " + "connection timeout=30");
SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * MY_TABLE_OF_OBJECTS", myConnection);
myAdapter.Fill(dataTableOfObjects);
myConnection.Close();
myConnection = null;
}
private void populateObjectDict(DataRow objDataRow)
{
ExampleBaseClass MyObject = new ExampleBaseClass();
MyObject.objectName = objDataRow["objectName"].ToString();
MyObject.prop1 = objDataRow["prop1"].ToString();
MyObject.prop2 = objDataRow["prop2"].ToString();
MyObject.prop3 = objDataRow["prop3"].ToString();
switch (objDataRow["ObjectType"].ToString())
{
case "Example1":
//set object specific properties
break;
case "Example2":
//set object specific properties
break;
dictmyObjects.Add(MyObject.objectName, MyObject);
}
}
}
I am stuck with the last method, populating the dictionary with my objects.
I want to have one MyObject which is set to the correct type based on the ObjectType from the database and then add this to the dictionary as I loop through. But I can't see to do this as MyObject will only ever have the properties of the base class. How do I turn it into one of the derived classes as I loop through? Or am I going about this in the wrong way?