How can I increment an integer one-at-a-time?

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

for (int i = 0; i < listBox2.Items.Count; i+=1)<<something like this?
{
thisString = (string)listBox10.Items;
}
Any help is appreciated.
Thanks,
Trint
 
Hi Trint,

use:

increment:
i++;
decrement
i--;

for (int i = 0; i < listBox2.Items.Count; i++)

sample one:

int i = 0;
Console.WriteLine(i++); <- value = 0
Console.WriteLine(i); <- value = 1

sample two: (value is incremented and then applied)
int i = 0;
Console.WriteLine(++i); <- value = 1 when printed

Hope this helps.

- Alex




- Alexander Rauser

http://www.flashshop.info
 
Sorry, not what I meant...I want it to go through the incrementation
just once until it comes back to the beginning from another call.
Can I just put a break; at the bottom "}" and store where I was at?
Trint

..Net programmer
(e-mail address removed)
 
I think you mean something like this

string getNext() {

if(currentIndex < listBox2.Items.Count)
listBox2[currentIndex]

}

Alex said:
Hi Trint,

use:

increment:
i++;
decrement
i--;

for (int i = 0; i < listBox2.Items.Count; i++)

sample one:

int i = 0;
Console.WriteLine(i++); <- value = 0
Console.WriteLine(i); <- value = 1

sample two: (value is incremented and then applied)
int i = 0;
Console.WriteLine(++i); <- value = 1 when printed

Hope this helps.

- Alex




- Alexander Rauser

http://www.flashshop.info
for (int i = 0; i < listBox2.Items.Count; i+=1)<<something like this?
{
thisString = (string)listBox10.Items;
}
Any help is appreciated.
Thanks,
Trint

 
I think you mean something like that:

public class {

private int currentIndex = 0;

void method() {

...

string str = getNext();
...

}

string getNext() {

if(currentIndex < listBox2.Items.Count) {
return listBox2[currentIndex++];
else {
currentIndex = 0;
return "";
}

}

}

So the answer is to declare a variable with a broader scope than a method
scope, an instance variabe for example and use it as index. In C there was a
static keyword you could use inside a function but it is not possible in C#.
 
Hi Trint,

Could you explain in some more detail?
You can provide the index as a parameter in the method, if that is what
you mean
Just once sounds a bit like you don't need a loop at all, much like robkit
mentions.
You can combine the two and to

void GetItem(int i)
{
if(i < listBox2.Items.Count);
thisString = (string)listBox10.Items;
}
 
There is obviously more to GetItem and GetNextItem...I have tried both
suggestions and don't quiet get how to write it...here is what I did:

string getNext()
{

if(currentIndex < listBox2.Items.Count)
{
return listBox2[currentIndex++];
}
else
{
currentIndex = 0;
return "";
}
Thanks for the answers though,
Trint
 
Back
Top