Home
Manage Your Code
Snippet: Auto-incrementing ID counter (C#)
Title: Auto-incrementing ID counter Language: C#
Description: A property that increments automatically each time it is accessed. Views: 189
Author: Justin T Date Added: 9/18/2007
Copy Code  
1      private int m_idCounter = 1;
2      /// <summary>
3      /// This is the IDCounter that will be used to designate a node's ID.
4      /// It will automatically increment each time it is accessed.
5      /// </summary>
6      public int IDCounter
7      {
8         get
9         {
10            // Increment the counter
11            m_idCounter = m_idCounter + 1;
12            // If this ID already exists, increment the counter.  Repeat as necessary.
13            while (idList.Contains(m_idCounter))
14            {
15               m_idCounter = m_idCounter + 1;
16            }
17            // Add this ID to the idList
18            idList.Add(m_idCounter);
19            return m_idCounter;
20         }
21         set { m_idCounter = value; }
22      }