469,329 Members | 1,438 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

How to make unique name given string and collection?

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;
}
}

Mar 10 '06 #1
5 2619
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
Mar 11 '06 #2
> 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.
Mar 11 '06 #3
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?
Use the Guid.NewGuid.ToString()

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
Mar 11 '06 #4
> 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.
Mar 11 '06 #5
On Sat, 11 Mar 2006 03:33:30 -0800, "deko" <de**@nospam.com> wrote:
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.

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
Mar 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Russell E. Owen | last post: by
5 posts views Thread by Paulers | last post: by
1 post views Thread by daldridge | last post: by
7 posts views Thread by André | last post: by
1 post views Thread by cedric.louyot | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.