increase values in a loop
-
hi guys is there a way i can automatically increaes a list of numbers by 1 e.g. 1,2,3,4,5 increase each one so its 2,3,4,5,6 Should I be using an array to do this or a loop The numbers are currently stored in a listbox thanks for any help cheers :)
I think there is no need to have any array Just check the last number and first number and iterate through it while incraesing the value. First chech Max
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "
-
I think there is no need to have any array Just check the last number and first number and iterate through it while incraesing the value. First chech Max
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "
Hey cheers for the replies, managed to get this to work: foreach (ListItem item in listboxact.Items) { string test = item.ToString(); int i_number = Int32.Parse(test); i_number++; string test2 = i_number.ToString(); listbox1.Items.Add(test2); } what do you think? it works but is there a better way?
-
Hey cheers for the replies, managed to get this to work: foreach (ListItem item in listboxact.Items) { string test = item.ToString(); int i_number = Int32.Parse(test); i_number++; string test2 = i_number.ToString(); listbox1.Items.Add(test2); } what do you think? it works but is there a better way?
Hi OK got another q now: with the loop is there a way to set it so it only runs for a certain few numbers e.g. i have 2,3,4,5,6 in the list and I want to loop through 3,4,5,6 but NOT 2. so somehting like foreach (ListItem item in listboxact.Items > 2) any help would be great cheers :)
-
hi guys is there a way i can automatically increaes a list of numbers by 1 e.g. 1,2,3,4,5 increase each one so its 2,3,4,5,6 Should I be using an array to do this or a loop The numbers are currently stored in a listbox thanks for any help cheers :)
If you are always following the pattern that you illustrate above, with the items sorted, you may want to consider simply removing the lowest item in the list and adding a new item. Something similar to the following code would allow you to achieve your goal without needing to loop at all.
int topValue = Int32.Parse(myDropDown.Items[myDropDown.Items.Count - 1].Value);
string newValue = topValue++.ToString();
myDropDown.Items.Add(new ListItem(newValue, newValue));
myDropDown.Items.RemoveAt(0);
Hope that helps. :)
--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
-
Hi OK got another q now: with the loop is there a way to set it so it only runs for a certain few numbers e.g. i have 2,3,4,5,6 in the list and I want to loop through 3,4,5,6 but NOT 2. so somehting like foreach (ListItem item in listboxact.Items > 2) any help would be great cheers :)
If you're always looking to skip the first item, then you could use a simple
for
loop to start at the second item in the list.for (int index = 1; index < myDropDown.Items.Count; index++)
{
string value = myDropDown.Items[index].Value;
// Work here.
}
If you're looking to skip arbitrary values, you could test for them and skip past them.
foreach (ListItem item in myDropDown.Items)
{
if (item.Value == "2")
{
continue;
}
// Work here.
}
Hope that helps. :)
--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
-
Hi OK got another q now: with the loop is there a way to set it so it only runs for a certain few numbers e.g. i have 2,3,4,5,6 in the list and I want to loop through 3,4,5,6 but NOT 2. so somehting like foreach (ListItem item in listboxact.Items > 2) any help would be great cheers :)
There is one more way Just Remove the first 0 the index and add one item at last index that should be grater that Last but one e.g you have 2 3 4 5 6 Remove 2 and add 7 at last index 3 4 5 6 7 got it
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "
-
If you're always looking to skip the first item, then you could use a simple
for
loop to start at the second item in the list.for (int index = 1; index < myDropDown.Items.Count; index++)
{
string value = myDropDown.Items[index].Value;
// Work here.
}
If you're looking to skip arbitrary values, you could test for them and skip past them.
foreach (ListItem item in myDropDown.Items)
{
if (item.Value == "2")
{
continue;
}
// Work here.
}
Hope that helps. :)
--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
hey Yeah I'm looking to do arbitrary values basically I want to run the loop for eahc number which is after a number the user has entered I tried the second example you put above and tried if (item.Value == "4") but it still increased every number rather then just the numbers after 4 why would this be? thanks for the help so far :)
-
Hi OK got another q now: with the loop is there a way to set it so it only runs for a certain few numbers e.g. i have 2,3,4,5,6 in the list and I want to loop through 3,4,5,6 but NOT 2. so somehting like foreach (ListItem item in listboxact.Items > 2) any help would be great cheers :)
Check this code Might be helpfull int num= Int32.Parse(ListBox1.Items[0].Text); // i know the total number of numbers are 4 ListBox1.Items.RemoveAt(0); num = num + 4; ListBox1.Items.Add(new ListItem(num.ToString()));
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "
-
Check this code Might be helpfull int num= Int32.Parse(ListBox1.Items[0].Text); // i know the total number of numbers are 4 ListBox1.Items.RemoveAt(0); num = num + 4; ListBox1.Items.Add(new ListItem(num.ToString()));
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "
I'm not sure if thats what i need basically let me explain again I have 1,2,3,4,5,6 someone wants to add the number 3 i need the number 3 to be added then the rest of the numbers including 3 increase by 1 1,2,3,4,5,6,7 I have foreach (ListItem item in listboxact.Items) { string test = item.ToString(); int i_number = Int32.Parse(test); i_number++; string test2 = i_number.ToString(); ListItem item2 = new ListItem(); listbox1.Items.Add(test2); } which increases every number by 1 is that clearer? :)
-
hey Yeah I'm looking to do arbitrary values basically I want to run the loop for eahc number which is after a number the user has entered I tried the second example you put above and tried if (item.Value == "4") but it still increased every number rather then just the numbers after 4 why would this be? thanks for the help so far :)
For that, you'll have to do more then check against a single number. The easiest way is to eat some casting cost and compare the current value to the one that you selected. Something like:
int controlValue = Int32.Parse(selectedValue);
int currentValue;
foreach (ListItem item in myDropDown.Items)
{
currentValue = Int32.Parse(item.Value);
if (currentValue < controlValue)
{
continue;
}
// Do Work
}
will increment only numbers
>=
your selected value.--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi
-
For that, you'll have to do more then check against a single number. The easiest way is to eat some casting cost and compare the current value to the one that you selected. Something like:
int controlValue = Int32.Parse(selectedValue);
int currentValue;
foreach (ListItem item in myDropDown.Items)
{
currentValue = Int32.Parse(item.Value);
if (currentValue < controlValue)
{
continue;
}
// Do Work
}
will increment only numbers
>=
your selected value.--Jesse
"... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi