|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to make unique name given string and collection?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; } } 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 > 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. 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 > 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.
Show quote
Hide quote
On Sat, 11 Mar 2006 03:33:30 -0800, "deko" <deko@nospam.com> wrote: If you're trying to make a unique name in a column of a data table, you could do>> 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. > 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
Other interesting topics
ZIP files in C#
c# CP210x Wrapper InteropServices IntPtr HandleRef problem Processing Files C# Books Embedding an image in a dll Using using verses specifying the namespace completely. Q: ColumnChanging How to validate fields on the form in C# TreeNode.Find() busted? convert a string to type for passing into a generic |
|||||||||||||||||||||||