Well, first of all, you should never catch an exception just to ignore it. Remove the try/catch block because you actually want to break the program flow when something goes wrong.
Passing strings with special meanings such as "@@@" is not a very good practice, it will make your code really hard to read, so you should also avoid it.
Are you sure that your exception is "Enumeration modified"? It does not seem that you are actually modifying the enumeration.
My suggestion is to use an IDictionary parameter instead of IEnumerator, it will give you .Count and .Contains(), so you don't have to copy it to a new list every time. Or you can at least use IEnumerable instead of IEnumerator to simplify your code a bit.
Try something like this:
-
public static string CreateUniqueID(IDictionary dictionary)
-
{
-
String id = UniqueID();
-
-
// Check if our dictionary already contains this key.
-
while (dictionary.Contains(id))
-
id = UniqueID();
-
-
return id;
-
}
You can also consider using System.Guid class, it generates random GUIDs so you don't have to do it yourself:
-
String guid = Guid.NewGuid().ToString();
And please use the CODE tags when posting ! :o)