How to make unique name given string and collection?

D

deko

Is there an accepted or standard way to get a unique name given a string and
the collection in which it needs to be unique? Should I use a HashTable?
Other options?

Here's a first crack:

private string makeUniqueName(string inName, MyCollection collection)
{
//if the name is not unique, append an indexer to the name
foreach (Item item in collection)
{
while (item.Text == inName)
{
string lastChar = inName.Substring(inName.Length - 1, 1);
int result;
int.TryParse(lastChar, out result);
bool lastCharIsNumeric = (result > 0 || lastChar ==
result.ToString());
result++;
if (lastCharIsNumeric)
{
inName = inName.Substring(0, inName.Length - 1);
inName = string.Concat(inName, result.ToString());
}
else
{
inName = string.Concat(inName, result.ToString());
}
}
}
string outName = inName;
//now index the name to make it unique, if it's not already unique
foreach (Item item in collection)
{
if (item.Text == outName)
{
outName = makeUniqueName(outName, collection);
}
}
return outName;
}
}
 
M

Michael Nemtsev

Hello deko,

Use the Guid.NewGuid.ToString()

d> Is there an accepted or standard way to get a unique name given a
d> string and the collection in which it needs to be unique? Should I
d> use a HashTable? Other options?
d>
d> Here's a first crack:
d>
d> private string makeUniqueName(string inName, MyCollection collection)
d> {
d> //if the name is not unique, append an indexer to the name
d> foreach (Item item in collection)
d> {
d> while (item.Text == inName)
d> {
d> string lastChar = inName.Substring(inName.Length - 1, 1);
d> int result;
d> int.TryParse(lastChar, out result);
d> bool lastCharIsNumeric = (result > 0 || lastChar ==
d> result.ToString());
d> result++;
d> if (lastCharIsNumeric)
d> {
d> inName = inName.Substring(0, inName.Length - 1);
d> inName = string.Concat(inName, result.ToString());
d> }
d> else
d> {
d> inName = string.Concat(inName, result.ToString());
d> }
d> }
d> }
d> string outName = inName;
d> //now index the name to make it unique, if it's not already unique
d> foreach (Item item in collection)
d> {
d> if (item.Text == outName)
d> {
d> outName = makeUniqueName(outName, collection);
d> }
d> }
d> return outName;
d> }
d> }
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
D

deko

Use the Guid.NewGuid.ToString()

Thanks for the tip. That sounds interesting, and perhaps I could use it,
but it's not exactly what I was after. To illustrate:

myFileName
myFileName1
myFileName2

When you add a new file to a directory in Windows, that file name is made
unique if it is not. I am trying to replicate this behavior in any
specified collection, given any particular string.
 
M

Michael Nemtsev

Hello deko,

Could you describe this more wide? I'm not cleary understand what are you
going to undertake?
To give unique name to file name you can generate names randomly.
But what the behaviour you want to get with collections?
d> Thanks for the tip. That sounds interesting, and perhaps I could use
d> it, but it's not exactly what I was after. To illustrate:
d>
d> myFileName
d> myFileName1
d> myFileName2
d> When you add a new file to a directory in Windows, that file name is
d> made unique if it is not. I am trying to replicate this behavior in
d> any specified collection, given any particular string.
d>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
D

deko

To give unique name to file name you can generate names randomly.

Yes, I could generate random names. But the idea is to preserve the input
string while appending an indexer to make it unique. The string could be
any string at all, not necessarily a file name.

So I guess my question is not really how to get a unique name, but rather
how to index a string, which is exactly what that routine I posted does,
however inelegant it may be.

Guid.NewGuid.ToString() could be used as a type of shadow indexer that makes
the user's input strings unique while appearing the same to the user. This
might be a better way to go rather than trying to enforce a naming
convention, though it would require another field in the DataTable.
 
O

Otis Mukinfus

Yes, I could generate random names. But the idea is to preserve the input
string while appending an indexer to make it unique. The string could be
any string at all, not necessarily a file name.

So I guess my question is not really how to get a unique name, but rather
how to index a string, which is exactly what that routine I posted does,
however inelegant it may be.

Guid.NewGuid.ToString() could be used as a type of shadow indexer that makes
the user's input strings unique while appearing the same to the user. This
might be a better way to go rather than trying to enforce a naming
convention, though it would require another field in the DataTable.
If you're trying to make a unique name in a column of a data table, you could do
something like this (not tested, just a theory):

select max(columnName) from tablename where columnName like 'yourTestName%',
then parse the numeric portion to determine the increment.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top