开发者

How can i make my count increment from zero respectively

开发者 https://www.devze.com 2023-02-26 12:38 出处:网络
I am having a condition as follows if (fedId == ftax) { index = 0; string strEntryDets = string.Empty; strEntryDets = EntryDetail().ToString();

I am having a condition as follows

if (fedId == ftax)
{
  index = 0;
  string strEntryDets = string.Empty;
  strEntryDets = EntryDetail().ToString();
  sbBatchHead开发者_JS百科erFile.AppendLine(strEntryDets);
}

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


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.


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消