Arane Arane - 21 days ago 6
C# Question

Get range in multiplication of 5

I have a number. For instance, my number is

19
. Then I want to populate my drop down with range in multiplication of 5. So my dropdownlist will consist of items of:
1-5

6-10

11-15

16-19


I tried modulus and division, however, I can't seems to get the range. Is there a fixed method?

Sample code

List<string> range = new List<string>();
int number = 19;
int numOfOccur = (19/5);

for (int i = 1; i < numOfOccur ; i++)
{
range.Add(i + " - " + (i * 5))
}

Answer

Sometime I think that old school code, without fancy linq is a bit more clear

int maximum = 19;
int multiple = 5;
int init = 1;
while (init + multiple < maximum )
{
    string addToDDL = init.ToString() + "-" + (init + multiple - 1).ToString();
    Console.WriteLine(addToDDL);
    init += multiple;
}
string last = init.ToString() + "-" + maximum.ToString();
Console.WriteLine(last);
Comments