For my program i need a great deal of completely random, unique, 12 character id's.
So i wrote these three functions
So when i need a unique id i simply go string s = GiveUniqueID(); , but every while and then the program will freeze because the exception "enumeratio n modified" keeps repeating in an endless loop.
I am passing the Keys.GetEnumera tor(); of dictionaries of varying value types and the count of the dictionary. Since the dictionary's are of varying types i cannot simply pass the dictionary as an argument.
So what i am asking is how to avoid this endless loop?
So i wrote these three functions
Code:
public static string GiveUniqueID(IEnumerator id_key, int count)
{
string id = "";
bool done = false;
while (!done)
{
id = CreateUniqueID(id_key, count);
if (id != "@@@")
done = true;
}
return id;
}
public static string CreateUniqueID(IEnumerator id_key, int count)
{
List<string> ids = new List<string>();
for(int i = 0; i < count; ++i)
{
try
{
id_key.MoveNext();
ids.Add((string)id_key.Current);
}
catch (Exception)
{
return "@@@";
}
}
bool done = false;
string id = "";
while (!done)
{
id = UniqueID();
if (!ids.Contains(id))
done = true;
}
return id;
}
public static string UniqueID()
{
string toreturn = Session.IdPrefix;
for (int i = 0; i != 10; ++i)
toreturn += Utility.RandomList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0).ToString();
return toreturn;
}
I am passing the Keys.GetEnumera tor(); of dictionaries of varying value types and the count of the dictionary. Since the dictionary's are of varying types i cannot simply pass the dictionary as an argument.
So what i am asking is how to avoid this endless loop?
Comment