自动生成字母数字唯一键 [英] Auto generate a alphanumeric uniquekey

查看:115
本文介绍了自动生成字母数字唯一键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我在一家运输公司的小项目中,我的问题是要生成一个字母数字代码(作业代码),该代码将使所有其他功能和模块正常工作,并且每次应递增
它仅是一个destktop应用程序,因此仅由于所有工作代码的字母组合都相同(例如"NFT"),因此如何仅获取最后的数值
我知道这很简单,但是由于我是一个初学者,所以我很难做到

hi all
Iam in a small project for a transportation company my problem is to generate an alpha numeric code(jobcode) based on it will be all other functions and modules work and every time it should increment
its a a destktop application only so how to get the last numeric values only since the alphabetic combination is same for all jobcode (something like "NFT")
I know it be simple but since a beginner I find it difficult can anyone give an idea

推荐答案

以回应Simon的解决方案并考虑到我的笔记(请参阅我对他的回答的评论),这是一个基于集中式存储库的解决方案,具有以下优点:

您可以使用某种数字类型的值,该值足以存储产品安装期间可能需要的尽可能多的ID,在每个ID生成时递增它并存储最后一个值.您可以很好地格式化结果以显示一些字母数字字符串.

这是这种格式的示例,以十六进制格式显示数字,并以-"分隔组:

In response to the solution by Simon and taking into account my notes (see my comments to his answer), here is a solution based on centralized repository which has its benefits:

You can have a value of some numeric type, sufficient to store as many IDs as you may need for the lifetime of the product installation, increment it on each ID generation and store the last value. You can nicely format the result to show some alphanumeric string.

This is an example of such formatting showing the number in hexadecimal format with groups delimited by ''-'':

static string FormatId(ulong id) {
    byte[] serialized = System.BitConverter.GetBytes(id);
    object[] parameters =
        System.Array.ConvertAll<byte, object>
            (serialized, new System.Converter<byte, object>(
                    (byteValue) => { return byteValue; }
            ));
    return string.Format("NFT#{7:X}{6:X}-{5:X}{4:X}-{3:X}{2:X}-{1:X}{0:X}", parameters);
}



如果您仍然使用C#v.2,则该版本不包含lambda表示法和类型推断(但具有泛型):



In case you still use C# v.2, this is the version without lambda notation and type inference (but with generics):

static string FormatId(ulong id) {
    byte[] serialized = System.BitConverter.GetBytes(id);
    object[] parameters =
        System.Array.ConvertAll<byte, object>
            (serialized, new System.Converter<byte, object>(
                    delegate(byte byteValue) { return byteValue; }
            ));
    return string.Format("NFT#{7:X}{6:X}-{5:X}{4:X}-{3:X}{2:X}-{1:X}{0:X}", parameters);
}



这是同一事物的版本,虽然性能可能不太好,但可能更容易理解.它也适用于C#v.2:



This is a version of the same thing which might be not so good in performance but might be easier to understand. It also works with C# v.2:

static string FormatId(ulong id) {
    byte[] serialized = System.BitConverter.GetBytes(id);
    object[] parameters = new object[serialized.Length];
    for(int index=0; index < serialized.Length; ++index)
        parameters[index] = serialized[index];
    return string.Format("NFT#{7:X}{6:X}-{5:X}{4:X}-{3:X}{2:X}-{1:X}{0:X}", parameters);
}



我不知道将十六进制格式与自定义格式说明符结合起来的更简单方法.使用十进制基数,它看起来就像:
return "NFT#" + id.ToString("0000-0000-0000-0000").

您可能希望使用较短的字符串和较少的ID范围.选择您喜欢的数字类型和格式,就可以完成.



通过以事务方式使用数据库,在一个事务中递增和返回ID,可以使ID生成在并发环境中可用.如果没有数据库,则可以保留当前数值,并使用lock进行获取ID线程安全的过程.



I don''t know a simpler way to combine hexadecimal format with custom format specifiers. With decimal base it would simply look:
return "NFT#" + id.ToString("0000-0000-0000-0000").

You may want to use shorted strings and less range of IDs. Choose the numeric type and format you prefer and you''re done.



You can make ID generation usable in concurrent environment by using your database in transactional manner, incrementing and returning ID in one transaction. Without a database, you can persist the current numeric value and make the procedure of getting the ID thread-safe using a lock.

class IdGenerator {
    void Load() { id = /*...*/ }
    void Store() { /*...*/ }
    static string FormatId(ulong id) { /*...*/ }

    //...

    string Id {
        get {
           ulong returnValue;
           lock (LockObject) {
              returnValue = ++id;
           }
           return FormatId(returnValue); //note: not under lock
        }
    }
    static ulong id;
    object LockObject = new object();
}



—SA



—SA


Guid.NewGuid()
我意识到它的字母数字,因为它包含-,但是要容易得多.如果由于某种原因GUID不够好,则需要在生成ID/代码的位置集中,因为您必须存储提供的最后一个ID/代码,以便对其进行递增.
这是一种可能的实现方式的伪代码.
Guid.NewGuid()
I realize it''s alpha numeric as it contains - but so much easier. if a guid is not good enough for one or the other reason you''ll need to have a centralized location where you generate the id/code, as you''ll have to store the last id/code provided so you can increment it.
Here is some pseudocode of one possible implementation.
int currentSystemTime = GetCurrentSystemTimeInTicks()
int count

ProventAnyOtherThreadsOrProcessesFromAccessing_Counter()
count = ++counter
AllowOtherThreadsToOrProcessesAccessing_Counter()

return ConcatenateStrings(IntToHex(currentSystemTime), IntToString(count))


这篇关于自动生成字母数字唯一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆