473,387 Members | 1,791 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,387 software developers and data experts.

unique id

cj
I need to choose a 20 char unique id for transactions handled by my
multithreaded program. I can't decide if to use

Dim uid As String = Guid.NewGuid.ToString
uid = uid.Replace("-", "")
uid = uid.Substring(0, 20)

or

Dim uid as string = Now().Ticks.ToString.PadLeft(20, "0")
Apr 26 '07 #1
7 2768
cj <cj@nospam.nospamwrote in news:u3XY4nBiHHA.1388
@TK2MSFTNGP05.phx.gbl:
I need to choose a 20 char unique id for transactions handled by my
multithreaded program. I can't decide if to use

Dim uid As String = Guid.NewGuid.ToString
uid = uid.Replace("-", "")
uid = uid.Substring(0, 20)
You don't need to do the replace line - the .ToString command has
different formatters:

http://msdn2.microsoft.com/en-us/lib....tostring.aspx

and in particular:

http://msdn2.microsoft.com/en-us/library/97af8hh4.aspx

Guid.NewGuid.ToString("N") will return an all numeric value.

Also you can't take a substring of a GUID and expect it to be unique...
A GUID is only unique if all 32 digits are present.
>
or

Dim uid as string = Now().Ticks.ToString.PadLeft(20, "0")
While the resolution of ticks is quite good, I'm not sure this can
gaurantee uniqueness as well. It really depends on how many concurrent
requests you're planning on handling.

Take a look at this:

http://www.codinghorror.com/blog/archives/000409.html

Shows you how to convert a 16byte GUID into a 20 byte ASCII
representation :-)

Apr 26 '07 #2
cj
Dim uid As String = Guid.NewGuid.ToString("N")
MessageBox.Show(uid)

returns letters too.
Spam Catcher wrote:
cj <cj@nospam.nospamwrote in news:u3XY4nBiHHA.1388
@TK2MSFTNGP05.phx.gbl:
>I need to choose a 20 char unique id for transactions handled by my
multithreaded program. I can't decide if to use

Dim uid As String = Guid.NewGuid.ToString
uid = uid.Replace("-", "")
uid = uid.Substring(0, 20)

You don't need to do the replace line - the .ToString command has
different formatters:

http://msdn2.microsoft.com/en-us/lib....tostring.aspx

and in particular:

http://msdn2.microsoft.com/en-us/library/97af8hh4.aspx

Guid.NewGuid.ToString("N") will return an all numeric value.

Also you can't take a substring of a GUID and expect it to be unique...
A GUID is only unique if all 32 digits are present.
>or

Dim uid as string = Now().Ticks.ToString.PadLeft(20, "0")

While the resolution of ticks is quite good, I'm not sure this can
gaurantee uniqueness as well. It really depends on how many concurrent
requests you're planning on handling.

Take a look at this:

http://www.codinghorror.com/blog/archives/000409.html

Shows you how to convert a 16byte GUID into a 20 byte ASCII
representation :-)
Apr 26 '07 #3
cj <cj@nospam.nospamwrote in news:#e**************@TK2MSFTNGP05.phx.gbl:
Dim uid As String = Guid.NewGuid.ToString("N")
MessageBox.Show(uid)

returns letters too.
There should be no dashes however.
Apr 26 '07 #4
cj
Correct.

I've discussed it here and since we've been using the first 20 of guid
I'm going to continue with that bad tradition. I can't count on ticks
to be unique either after all and I need max 20 and I don't feel like
working with another component like ASCII85.

Thanks for you help.

Spam Catcher wrote:
cj <cj@nospam.nospamwrote in news:#e**************@TK2MSFTNGP05.phx.gbl:
>Dim uid As String = Guid.NewGuid.ToString("N")
MessageBox.Show(uid)

returns letters too.

There should be no dashes however.
Apr 26 '07 #5
cj <cj@nospam.nospamwrote in
news:#F**************@TK2MSFTNGP05.phx.gbl:
Correct.

I've discussed it here and since we've been using the first 20 of guid
I'm going to continue with that bad tradition. I can't count on ticks
to be unique either after all and I need max 20 and I don't feel like
working with another component like ASCII85.
You shoudl just an incrementing counter ;-)
Apr 26 '07 #6
On Apr 26, 3:40 pm, cj <c...@nospam.nospamwrote:
I've discussed it here and since we've been using the first 20 of guid
I'm going to continue with that bad tradition. I can't count on ticks
to be unique either after all and I need max 20 and I don't feel like
working with another component like ASCII85.
You can't count on the first 20 characters of a guid to be unique
either. Why can't you use the entire guid? That you can count on to
be unique.
Apr 27 '07 #7
cj
Field I'm putting it in is 20 chars.

Like I said however if they've been getting by with the first 20 chars
in the past I'll just continue the bad practice.
Chris Dunaway wrote:
On Apr 26, 3:40 pm, cj <c...@nospam.nospamwrote:
>I've discussed it here and since we've been using the first 20 of guid
I'm going to continue with that bad tradition. I can't count on ticks
to be unique either after all and I need max 20 and I don't feel like
working with another component like ASCII85.

You can't count on the first 20 characters of a guid to be unique
either. Why can't you use the entire guid? That you can count on to
be unique.

Apr 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kevin parks | last post by:
hi. I've been banging my head against this one a while and have asked around, and i am throwing this one out there in the hopes that some one can shed some light on what has turned out to be a...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
3
by: June Moore | last post by:
Hi, I would like to add a unique index that consists of two fields in a table. e.g. tbl_A (field1,field2) -- field1 & field2 Indexed and combination must be Unique. Can anyone tell me the...
5
by: Kamil | last post by:
Hello What should I use for better perfomance since unique constraint always use index ? Thanks Kamil
6
by: Bob Stearns | last post by:
I was under the impression that the primary key had to be a unique index. Since I usually create my primary indices before my primary keys, in order to get the indices in the same schema as their...
4
by: bwmiller16 | last post by:
Guys - I'm doing a database consistency check for a client and I find that they're building unique indexes for performance/query reasons where they could be using non-unique indexes. Note...
5
by: aj | last post by:
DB2 WSE 8.1 FP5 Red Hat AS 2.1 What is the difference between adding a unique constraint like: ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE ( <COL1>) ; and adding a...
7
by: Brian Keating | last post by:
Hi there, Is it possible to add a unique constraint on two columns in a table, so that the constraint is a composite of the two? i.e. these two columns together should be unique...? i.e....
10
by: Laurence | last post by:
Hi there, How to differentiate between unique constraint and unique index? These are very similar but I cannot differentiate them? Could someone give me a hand? Thanks in advance
2
by: pstachy | last post by:
Hi again! I have another issue. I would like the attribute of the tag <invoice> to be unique. Made the following schema but unfortunately it doesn't validate. Could someone please indicate what is...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.