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

  • Home
  • SEARCH
  • 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 8736665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:21:17+00:00 2026-06-13T10:21:17+00:00

I was facing this problem earlier today, and since I could not find a

  • 0

I was facing this problem earlier today, and since I could not find a satisfactory solution, I decided to change my class design, and have seperate properties such as Tag 1, Tag 2, Tag 3 etc.

My main problem is the fact that I need to bind a grid to an object that contains a list among other properties and I need to show each item in the list as a separate column which I am unable to do. Hence I am resorting to declaring variables separately. Original question is here…

Now, I’m facing one of the most common design problem that probably every programmer has at some point of time. Here is the code to demonstrate it,

for (int i = 0; i < tags.Length; ++i) // Length not known here.
{
    if(i==0){
        tag1 = tags[0];
    } 
    else if(i == 1){
        tag2 = tags[1];
    }
    else if(i == 2){
        tag3 = tags[2];
    }
    ....
}

Here tags is a string array.

I was wondering if there is a more elegant way to do this. Another thing to note is that the efficiency of this loop decreases as it progresses, since with more iterations it has to check more conditions. If we could remove a condition after it had become true once it would speed up each iteration since we know that each condition will become true only once in all the iterations

  • 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-13T10:21:18+00:00Added an answer on June 13, 2026 at 10:21 am

    Moved answer about DataGridView and using ComponentModel to the correct question:
    Displaying a list of object containing a list in a grid view

    Briefing

    The DataGridView controll supports the ComponentModel namespace so that you can create classes that appear to have properties that don’t exist. It is the same mechanism the PropertyGrid uses.

    The sample code is in this answer of that question:
    https://stackoverflow.com/a/13078735/195417

    OLD ANSWER

    This was my previous answer, when I didn’t realize the real question was about the DataGridView control.


    Isn’t this the same as setting the values directly:

    this.tag1 = tags[0];
    this.tag2 = tags[1];
    this.tag3 = tags[2];
    

    EDIT: as you sayd you don’t know how many variables will be needed, then you need only one, and that is a list:

    var list = new List<string>();
    for (int i = 0; i < tags.Length; ++i)
    {
        list.add(tags[i]);
    }
    

    If all you want is to copy all values, you can even do this:

    var list = new List<string>(tags);
    

    Tell me whether this is what you want or not… maybe I have misunderstood the question.

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

Sidebar

Related Questions

I'm facing this problem again and again, but can't find any solution (except mine,
I'm facing this problem and could not get through by any means. Here is
I'm probably not the first one facing this problem, but I couldn't find a
I am facing this problem and finding solution for this issue since last 2
This is a design problem I am facing. Let's say I have a cars
I am facing this problem class person { ; } person p = new
I am facing this problem that buttons in android are not clickable when given
Am facing this problem ever since i upgraded to XCODE 4.5. I have various
Now I'm facing this problem and don't know about a solution yet. The doc
I am facing this problem with only Windows Phone solutions. My old solution for

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.