Click here to Skip to main content
15,923,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below list
C#
List<int> lstuser = new List<int>();
lstuser.Add(101);
lstuser.Add(102);
lstuser.Add(103);
lstuser.Add(104);

int assigntovariable =0;

I need to assign the above list values to a variable subsequently. That is variable will be inside a for loop & and its length may vary from 1 to any.

for (int i =0;i<10;i++)
{
assigntovariable = //lstuser from one by one in order as
assignedtouser = 101, if i = 0
assignedtouser = 102, if i = 1
assignedtouser = 103, if i = 2
assignedtouser = 104, if i = 3
assignedtouser = 101, if i = 4
assignedtouser = 102, if i = 5

..it will continue till the end of loop

}

Below is what i tried ..but its not good one i think.. please provide me a better solution..Thanks in advance..

C#
<pre lang="C#">List&lt;int&gt; lstuser = new List&lt;int&gt;();
            lstuser.Add(101);
            lstuser.Add(102);
            lstuser.Add(103);
            lstuser.Add(104);
            int userselected = 0;
            int j = 0, x = 0;

            for (int i = 0; i &lt; 10; i++)
            {
                if (x == 0 || x == lstuser.Count)
                     x = 0;
                for (j = x; j &lt; lstuser.Count; j++)
                {
                    if (i &gt; j)
                    {
                        userselected = lstuser[j];
                        if (x == lstuser.Count)
                            x = 0;
                        x = j + 1;
                    }
                    else
                    {
                        userselected = lstuser[i];
                        x = j+1;
                        if (x == lstuser.Count)
                            x = 0;
                    }
                    break;
                }

                int s = userselected;
            }</pre>


What I have tried:

C#
List<int> lstuser = new List<int>();
            lstuser.Add(101);
            lstuser.Add(102);
            lstuser.Add(103);
            lstuser.Add(104);
            int userselected = 0;
            int j = 0, x = 0;

            for (int i = 0; i < 10; i++)
            {
                if (x == 0 || x == lstuser.Count)
                     x = 0;
                for (j = x; j < lstuser.Count; j++)
                {
                    if (i > j)
                    {
                        userselected = lstuser[j];
                        if (x == lstuser.Count)
                            x = 0;
                         x = j + 1;
                    }
                    else
                    {
                        userselected = lstuser[i];
                        x = j+1;
                        if (x == lstuser.Count)
                            x = 0;
                    }
                    break;
                }

                int s = userselected;
            }
Posted
Updated 4-Jul-16 20:41pm
v3

1 solution

The easiest way is to use the modulus operator '%'
C#
List<int> lstuser = new List<int>();
lstuser.Add(101);
lstuser.Add(102);
lstuser.Add(103);
lstuser.Add(104);
for (int i = 0; i < 10; i++)
   {
   int assignedToUser = lstUser[i % lstUser.Count];
   ...
   }

The Modulus operator returns the remainder after a division: so x % 10 will always be 0 to 9, x % 3 will be 0 to 2, and x % lstUser.Count will be 0 to (number elements in the List minus one), which is what you need for an index into the list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900