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

Community
  • 1
  • 1
Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64

3 Answers3

7

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.

Community
  • 1
  • 1
Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
  • 1
    Ok, sorry! I shouldn't be saying this... maybe the guy is just learning yet. – Miguel Angelo Oct 25 '12 at 20:47
  • @MiguelAngelo Thanks for that cheeky comment, really helped to boost my confidence. And no I'm not a beginner. If you would be so kind to answer the updated question, I'd love to get the hell out of here and cry myself to sleep... – Abijeet Patro Oct 25 '12 at 20:58
  • @MiguelAngelo You understood the original question correctly which was wrong, but failed to understand my corrected question. 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. Hence I am resorting to declaring variables separately. I should have explicitly mentioned my requirements, my mistake. Here is the original question.. http://stackoverflow.com/questions/13064156/displaying-a-list-of-object-containing-a-list-in-a-grid-view – Abijeet Patro Oct 25 '12 at 21:15
  • I'll change my answer in a minute! I understood it... sorry I was blind for a moment. – Miguel Angelo Oct 25 '12 at 21:20
  • What control is it? A DataGridView? – Miguel Angelo Oct 25 '12 at 21:22
  • This was a pretty stupid question. Atleast I made everyone laugh. Sigh. I'm going to limit the user to enter a maximum of 4 tags, and use this method to put the value in the 4 tag variables. If the user enters only two tags, I'll initialize the rest of the values to empty string. – Abijeet Patro Oct 25 '12 at 21:57
  • @MiguelAngelo if you could post this answer over in the original question, and unstrike the actual answer here, it would help other people more. Thanks a lot for taking the time out to answer the original question. :) – Abijeet Patro Oct 25 '12 at 22:11
1

The whole loop is pointless. But unless the tags array length is always going to be the same, you have to be sure not to go out of bounds...

if(tags.Length >= 1) this.tag1 = tags[0];
if(tags.Length >= 2) this.tag2 = tags[1];
if(tags.Length >= 3) this.tag3 = tags[2];
if(tags.Length >= 4) this.tag4 = tags[3];
if(tags.Length >= 5) this.tag5 = tags[4];

... so on for however many this.tag# you have.

Mr.Mindor
  • 4,079
  • 2
  • 19
  • 25
  • This would work somewhat. But still I have no idea how many variables I'll have to declare beforehand. – Abijeet Patro Oct 25 '12 at 21:32
  • ok then, what is 'this' pointing to? At what point is tag1. tag2. tag3 declared (as opposed to assigned to) Are you looking to dynamically create the underlying class so the number of tagX fields changes? or will there be a set # of tagX fields that can be used. – Mr.Mindor Oct 25 '12 at 21:40
  • If you want your underlying class to have tag1, tag2, tag3 properties this time, but tag1,tag2,tag3,tag4 properties next time without modifying your source code, and without recompiling you need something like this: http://stackoverflow.com/q/2698619/391656 . If however your class always has tag1 through tag9, then you know how many variables you have. you are not changing the number declared, only need to control how many you assign into. – Mr.Mindor Oct 25 '12 at 21:54
-3

This is essentially the same:

for(int index = 0; index < tags.Length[]; index++){
    switch(index){
        case 0:
            tag1 = tags[0];
            break;
            // And so on
    }
}
James Michael Hare
  • 37,767
  • 9
  • 73
  • 83
Graknol
  • 55
  • 1
  • 8
  • 2
    Unfortunately, it's no better... Okay, *marginally* better because it may use a jump-table behind the scenes instead of an if-ladder, but still unnecessary when one can just simply do as @MiguelAngelo suggests, or just *use the array* – James Michael Hare Oct 25 '12 at 20:43
  • Yeah, I know that, but I didn't know if he had to use the loop (for some reason) – Graknol Oct 25 '12 at 20:46
  • 1
    Not to assign the variables, which was the main thrust of his question. He can still have the loop, but assigning the variables to the contents of each index through the loop is very sub-optimal at best. – James Michael Hare Oct 25 '12 at 20:47
  • And again for(int index = 0; index < tags.Length[]; index++), it won't compile. – Nikola Davidovic Oct 25 '12 at 20:52