PC Review


Reply
Thread Tools Rate Thread

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

 
 
trint
Guest
Posts: n/a
 
      7th Feb 2005
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

 
Reply With Quote
 
 
 
 
Adam
Guest
Posts: n/a
 
      7th Feb 2005
+=1 will work just fine; although, the ++ operator provides the same
functionality.

 
Reply With Quote
 
Alex
Guest
Posts: n/a
 
      7th Feb 2005
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
>

 
Reply With Quote
 
Trint Smith
Guest
Posts: n/a
 
      7th Feb 2005
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 Removed)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
=?Utf-8?B?cm9ia2l0?=
Guest
Posts: n/a
 
      7th Feb 2005
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
> >

>

 
Reply With Quote
 
=?Utf-8?B?cm9ia2l0?=
Guest
Posts: n/a
 
      7th Feb 2005
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
>
>

 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      7th Feb 2005
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 <(E-Mail Removed)>
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
> (E-Mail Removed)
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!




--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
trint
Guest
Posts: n/a
 
      7th Feb 2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto Increment for non integer ? saleemMSMS Microsoft Access Database Table Design 2 12th Aug 2009 01:39 PM
How to auto increment integer entries without autonumber in 2007 Arog Microsoft Access 3 23rd Jun 2009 04:03 AM
increment an integer in a formula with the fill function vexed Microsoft Excel Worksheet Functions 3 6th Jun 2008 05:20 PM
increment an integer variable by 1 Gary Microsoft Access 5 19th Oct 2005 09:13 AM
checking of time entry falls between time increment eric Microsoft Access VBA Modules 1 14th Dec 2003 07:39 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:44 PM.