Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:43:13+00:00 2026-06-17T08:43:13+00:00

How can I increment an integer inside a foreach loop (like in C++) This

  • 0

How can I increment an integer inside a foreach loop (like in C++)

This is my code, but it does not increment the selectIndex integer during each loop iteration.

var list = new List<string>();
int selectIndex = 0;

foreach(TType t in Gls.TTypes)
{
    selectIndex = Gls.TType.Name == t.Name ? selectIndex++ : 0;
    list.Add(t.Name);
}

Got it working as follows:

var list = new List<string>();
int selectIndex = 0;
int counter = 0;
foreach(TaxType t in Globals.TaxTypes)
{
    selectIndex = Globals.TaxType.Name == t.Name ? counter : selectIndex;
    counter++;

    list.Add(t.Name);
}

The purpose was to select the matching item in a UIPickerView.

Many thanks to all contributions!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:43:14+00:00Added an answer on June 17, 2026 at 8:43 am

    IMHO that pattern you are using here is horrid. Pre and postfix increments modify the value they’re invoked upon so it makes no sense to then copy the result (incidentally it’s not working because you’re copying the value before the postfix increment takes place).

    So you can use solutions like @Vilx- and @KarthikT’s – but in my opinion, instead of trying to cram it all on one line I’d much rather see:

    if(Gls.TType.Name == t.Name) 
      selectIndex++;
    else selectIndex = 0;
    

    Don’t get me wrong – I use the conditional operator a lot; but I wouldn’t in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I increment an integer value outside the scope of a parallel.foreach loop?
Can a VB.NET For loop be constructed that mimics this C# code? TimeSpan oneDay
I'm creating two tables like this: CREATE TABLE abc ( id INTEGER NOT NULL
I have two MySQL tables like this: User Id (PK, auto-increment integer) Subscriber Id
I know that I can set up an integer before the loop and increment
All, How Can we increment a value like the following in django templates, {{
I know in an actual query, you can use auto_increment=1 but how is this
How do I increment a Integer's value in Java? I know I can get
This is my schema: CREATE TABLE item ( id integer NOT NULL PRIMARY KEY
I have a for loop and inside that integer x will increase from 533

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.