This is a winforms c # application.
Introduction:
I am creating a form that will allow users to import data from an existing MySql or SQL Server database into my SQL Server database. This allows users to quickly import IP addresses and other data without re-entering it through the control.
Example:
Simplified, I have two objects, FieldObject and SensorObject . FieldObject exists to store the names and types of fields in the source and destination database. SensorObject is an object that I later populate from records in the database. For simplicity, I omit type handling and other functions that are not relevant to the question. (The data for the DestinationField limited to the list that I provide to the user, and comes from an array or list inside the program.) Here is an example of both:
public class FieldObject { public string DestinationField {get; set;} public string SourceField {get; set;} } public class SensorObject { public string Name {get; set;} public string IPAddress {get; set;} }
Problem:
When a user fills in various FieldObject fields, I use this information to populate the FieldObject database, although I have a large switch statement that checks the name of the SensorObject field to find out which SensorObject property it belongs to.
For example:
// Reader is a SqlDataReader with the prerequisite database connection FieldObject myField = new FieldObject { DestinationField = "name", SourceField = "proprietary" }; SensorObject mySensor = new SensorObject(); switch (myField.DestinationField) { case "name": mySensor.Name = Convert.ToString(Reader[myField.DestinationField]); break; case "ip_address": mySensor.IPAddress = Convert.ToString(Reader[myField.DestinationField]); break; }
As you can see, processing more properties for an object will require more redundant code.
Question:
I would like to somehow preserve the SensorObject property to which the data belongs, so when repeating FieldObjects in the list, I can effectively exclude the switch statement.
Something like:
foreach(FieldObject f in myFieldList) { mySensor(f.mySensorField) = Convert.ToString(Reader[f.DestinationField]); }
I'm not sure what technique in C # lends itself to this kind of application. I looked at reference values ββand reflection, and they do not seem to be suitable.
I appreciate understanding, advice, and even ways to rethink this approach.
object c # properties winforms
Jyelton
source share