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 9256197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:53:04+00:00 2026-06-18T11:53:04+00:00

I have a list of strings like: public string TypeOrig { get; set; }

  • 0

I have a list of strings like:

public string TypeOrig { get; set; }
public string TypeAdj { get; set; }
public string TypeClass { get; set; }
public string TypeMsg { get; set; }
public string QtyOrig { get; set; }
public string QtyAdj { get; set; }
public string QtyClass { get; set; }
public string QtyMsg { get; set; }

Based on the row from the db, I populate the strings like this:

switch(fRow["fieldName"].ToString())
{
    case "partType":
        TypeOrig = fRow["original"].ToString();
        TypeAdj = fRow["adjusted"].ToString();
        TypeClass = fRow["status"].ToString();
        TypeMsg = fRow["message"].ToString();
        break;
    case "qty":
        QtyOrig = fRow["original"].ToString();
        QtyAdj = fRow["adjusted"].ToString();
        QtyClass = fRow["status"].ToString();
        QtyMsg = fRow["message"].ToString();
        break;
}

I would like to reduce this to something like this:

switch(fRow["fieldName"].ToString())   
    case "partType": fieldName = "Type";
        break;
    case "qty": fieldName = "Qty";
        break;
}
fieldName + "Orig" = fRow["original"].ToString();
fieldName + "Adj" = fRow["adjusted"].ToString();
fieldName + "Class" = fRow["status"].ToString();
fieldName + "Msg" = fRow["message"].ToString();

Additional Information:

  1. There are a few exceptions from the field name in the db, so I cannot simply use the field name and have to use the switch.
  2. I also have to populate some non-standard strings for a couple of the fields from the db.
  3. I currently have 32 field names and that can grow, so reducing the length of code will make it so much easier to manage. (@Servy’s approach reduced it down to 19)
  4. The resulting strings are used in the view.

How do I dynamically set the string name and populate it?

  • 1 1 Answer
  • 0 Views
  • 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-06-18T11:53:05+00:00Added an answer on June 18, 2026 at 11:53 am

    You should refactor your object. Whenever you see yourself prefixing a bunch of fields/properties with the same thing it often manes you should be creating a new type to represent that kind of data.

    public class Foo //TODO give meaningful name
    {
        //I changed the abbreviations to real words.  If I was wrong, please fix, but
        //as a rule try to avoid overuse of abbreviations in variable names; 
        //it makes code harder to read.
        public string Original { get; set; }
        public string Adjacent { get; set; }
        public string Class { get; set; }
        public string Message{ get; set; }
    }
    

    Now back to your first type we can have:

    public class MyClass //TODO fix name
    {
        public Foo Type {get;set;}
        public Foo Quality {get;set;}
    }
    

    Now that we have two objects of the same type we can separeate out the code for populating the object and placing that object in MyClass:

    Foo foo = new Foo()
    {
        Original = fRow["original"].ToString(),
        Adjacent = fRow["adjusted"].ToString(),
        Class = fRow["status"].ToString(),
        Message= fRow["message"].ToString(),
    };
    
    switch(fRow["fieldName"].ToString())
    {
        case "partType":
            Type = foo;
            break;
        case "qty":
            Quality = foo;
            break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of parameters like this: public class parameter { public string
E.G. to create an ArrayList of Strings we have to do something like List<String>
I have a known list of strings like the following: List<string> groupNames = new
I have list of strings like this FirstName-Lastname (separated by a dash -) I
I have a list of strings like this: my_list = ['Lorem ipsum dolor sit
Say I have a list containing strings that look like this: 00x000s00g00wfds0d dkdf00sdf00sdfg00jk kf00dfjkd0sdl0sd0f0
I have the list with like 100,000 site link strings Each link is unique,
I have some LINQ code that generates a list of strings, like this: var
I have a list of string like: s = [(abc,bcd,cde),(123,3r4,32f)] Now I want to
I have a string like this: TEST.DATA.Data.COR.Point,2;TEST.DATA.Data.COR.Point,5;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.WordTOFIND,18 I have a list of array with

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.