Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1011533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:14:56+00:00 2026-05-16T09:14:56+00:00

This is a C# winforms app. Preface: I am creating a form that will

  • 0

This is a C# winforms app.

Preface:

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 having to re-enter it via a control.

Example:

Simplified, I have two objects, FieldObject and SensorObject. FieldObject exists to store the source and destination database field names and types. SensorObject is the object I later populate from the records in the database. For simplicity’s sake, I am omitting the type handling and other functionality that is not relevant to the question. (The data for DestinationField is limited to a list that I provide the user, and comes from an array or list within 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 the user populates the various fields of FieldObject, I use the information to populate the destination database, though I have a large switch statement that checks the destination field name to know what property of SensorObject it belongs.

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 it would require more redundant code to handle more properties for the object.

Question:

I’d like some way of storing the property of SensorObject that the data belongs to, so that when iterating FieldObjects in a list, I can effectively eliminate the switch statement.

Something like:

foreach(FieldObject f in myFieldList)
{
    mySensor(f.mySensorField) = Convert.ToString(Reader[f.DestinationField]);
}

I am not certain what technique in C# lends itself to this sort of application. I’ve looked into reference values and reflection, and neither seem appropriate.

I appreciate any insight and advice or even ways to rethink this approach.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:14:56+00:00Added an answer on May 16, 2026 at 9:14 am

    You have to use reflection to do this.
    Should end up as something like:

    var sensorFields = typeof(SensorObject).GetProperties()
    foreach(var field in fields)
    {
        var info = sensorFields.First(sensorField => sensorField.Name == field.Name);
        var value = Convert.ToString(Reader[field.Destination]);
        info.SetValue(sensorObj, value, new object[0]);
    }
    

    GetProperties gets a property info for each property which can be used to set the value.

    Of course property infos can be cached. Just write it once and refactor as soon as it runs. Don’t over complicate too much this is called premature optimization and leads straight to hell 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I have a Winforms app with a Dictionary that looks like this:
I developed a winforms app locally on my machine. It reads data from a
This application has a WCF web service that is called by a WinForms app.
I'm making a winforms app in vb.net that connects to a mysql database on
I am developing a Winforms app using C# and VS 2010. This app will
I have a winforms app that installs other apps in a loop. This works
In this case, the source of the text is from a winforms textbox. I'm
I've got a ComboBox on a winforms app with this code: comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
I have a Winforms App in C# with a ListView control. This ListView shows
we have WinForms app which stores data in SQL Server (2000, we are working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.