Vivekh
Vivekh

Reputation: 4259

How can i make my count increment from zero respectively

I am having a condition as follows

if (fedId == ftax)
{
  index = 0;
  string strEntryDets = string.Empty;
  strEntryDets = EntryDetail().ToString();
  sbBatchHeaderFile.AppendLine(strEntryDets);
}

When this statement is true i should increment the index value starting from zero can any one give me an idea

Upvotes: 0

Views: 297

Answers (2)

Sonosar
Sonosar

Reputation: 526

I don't know what is your iteration logic and when you are using all this stuff thats why I'm giving this suggestion.

Add this class to your application:

  public class IndexHolder
    {
        private static int m_Index = -1;
        public static int GetNext()
        {
            return m_Index++;
        }
        public static void Reset()
        {
            m_Index = -1;
        }
    }

In your code use as this :

index = IndexHolder.GetNext();

Upvotes: 1

Kon
Kon

Reputation: 27431

Set it to zero outside the if block. Also, what's the point of setting strEntryDets to an emptry string if you're just going to set it again on the very next line? In fact, you can just avoid setting that variable altogether.

index = 0;

if (fedId == ftax)
{
  index++;
  sbBatchHeaderFile.AppendLine(EntryDetail().ToString());
}

If you need to use index (starting at zero) and THEN increment it, you can do this:

if (fedId == ftax) 
{
  foo(index++);
}

This will pass in 0 and then increment it to 1.

Upvotes: 2

Related Questions