Auto-incrementing ID counter

Settembre 19th, 2007

Description: A property that increments automatically each time it is accessed.

Link: http://www.codekeep.net/snippets/7440fda7-0c38-42ca-8645-af46eae49379.aspx

      private int m_idCounter = 1;
      /// <summary>
      /// This is the IDCounter that will be used to designate a node's ID.
      /// It will automatically increment each time it is accessed.
      /// </summary>
      public int IDCounter
      {
         get
         {
            // Increment the counter
            m_idCounter = m_idCounter + 1;
            // If this ID already exists, increment the counter.  Repeat as necessary.
            while (idList.Contains(m_idCounter))
            {
               m_idCounter = m_idCounter + 1;
            }
            // Add this ID to the idList
            idList.Add(m_idCounter);
            return m_idCounter;
         }
         set { m_idCounter = value; }
      }

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.