469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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

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

Nov 16 '05 #1
7 19556
+=1 will work just fine; although, the ++ operator provides the same
functionality.

Nov 16 '05 #2
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

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

Nov 16 '05 #3
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
tr***********@gmail.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
I think you mean something like this

string getNext() {

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

}

"Alex" wrote:
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

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

Nov 16 '05 #5
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#.

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

Nov 16 '05 #6
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[i] = (string)listBox10.Items[i];
}

On Mon, 07 Feb 2005 05:19:56 -0800, Trint Smith <tr***********@gmail.com>
wrote:
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
tr***********@gmail.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7
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

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gena | last post: by
3 posts views Thread by Joseph Hill | last post: by
7 posts views Thread by mhk | last post: by
8 posts views Thread by Mantorok Redgormor | last post: by
38 posts views Thread by Keith | last post: by
17 posts views Thread by henrytcy | last post: by
reply views Thread by Michele 'xjp' | last post: by
6 posts views Thread by truezplaya | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.