473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2762
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Russell E. Owen | last post by:
I have several situations in my code where I want a unique identifier for a method of some object (I think this is called a bound method). I want this id to be both unique to that method and also...
1
by: hikums | last post by:
I am posting this here, just in case anyone may need this. Step 1: CREATE SEQUENCE ID_SEQ START WITH 1050000 INCREMENT BY 1 MAXVALUE 9999999 NO CYCLE NO CACHE ORDER
1
by: theyas | last post by:
I have a page that I get to by selecting the part number and serial number of an item in my database. The pages then generates a set of files. Some of these files have unique names (containing...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
4
by: Kyote | last post by:
I'm trying to persist a list of filenames. I've made a custom collection and a FileName class: 'Class to hold file name information Public Class FileNames Public fullName As String Public...
1
by: daldridge | last post by:
I have a unique-elements/sorting question (who doesn't?), but haven't yet been able to get appropriate template/select/for-each processing working. I don't fully grok the Muenchian technique yet...
7
by: André | last post by:
Hi, I need several cookies depending of an variable (x), so i defined a HttpCookie() as an array. My problems: 1)I get the error: Object reference not set to an instance of an object. 2)My...
0
by: Torsten Munkelt | last post by:
Hi, I want to write an XML-schema saying that this document <root> <edge type="special"> <target type="one"/> </edge> <edge type="special"> <target type="one"/>
1
by: cedric.louyot | last post by:
Hi, I've written a schema that looks like : <xs:schema> <xs:complexType name="myType"> <xs:sequence> <xs:element name="e1" type="T1" maxOccurs="unbounded"/> <xs:element name="e2"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.