473,586 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Substrin g(inName.Length - 1, 1);
int result;
int.TryParse(la stChar, out result);
bool lastCharIsNumer ic = (result > 0 || lastChar ==
result.ToString ());
result++;
if (lastCharIsNume ric)
{
inName = inName.Substrin g(0, inName.Length - 1);
inName = string.Concat(i nName, result.ToString ());
}
else
{
inName = string.Concat(i nName, 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 2777
Hello deko,

Use the Guid.NewGuid.To String()

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.Substrin g(inName.Length - 1, 1);
d> int result;
d> int.TryParse(la stChar, out result);
d> bool lastCharIsNumer ic = (result > 0 || lastChar ==
d> result.ToString ());
d> result++;
d> if (lastCharIsNume ric)
d> {
d> inName = inName.Substrin g(0, inName.Length - 1);
d> inName = string.Concat(i nName, result.ToString ());
d> }
d> else
d> {
d> inName = string.Concat(i nName, 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.To String()

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.To String()

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.To String() 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.co m> 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.T oString() 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
2690
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 stable (so I can regenerate it later if necessary). I thought the id function was the obvious choice, but it doesn't seem to work. The id of two...
1
11234
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
1930
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 part/serial data within the name) and others of these files (for historical/hysterical reasons) always have the same name (regardless of which item...
5
28334
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 each element but I am having issues using redim to adjust the new array. Any help or example code would be greatly appreciated. thanks!
4
2768
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 fileName As String Public fileExtention As String Public filePath As String Public newName As String
1
1679
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 (still an XSLT n00b), but I'm not sure that's the way to go anyway... What I'm trying to accomplish is generation of XML Schema output from a...
7
2878
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 second question is: how to request those cookies, because there is no name? Thanks
0
2553
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
4016
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" type="xs:string"/> </xs:sequence>
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.