【问题标题】:How to create Alphanumeric Auto Increment [closed]如何创建字母数字自动增量[关闭]
【发布时间】:2014-07-16 03:23:20
【问题描述】:

有人可以指导我或至少给我教程链接我想创建一个字母数字自动递增 ID/代码,格式如下 123-A00000010-A12,123 和 A12 是恒定的,A00000010 是自动递增的。

【问题讨论】:

  • 样本数据? 123-A99999999-A12 之后会发生什么?哦,是的 - 你尝试过什么
  • 这个网站是用来提问的,它不是免费的编码服务。
  • 只有您的程序会管理 ID 生成?
  • B00000001 我认为我的数据库无法达到那么远
  • @Blorgbeard 我不要求提供代码,我只要求提供想法或教程,如果有的话最好。我已经有了一个想法,但两个脑袋总比一个脑袋好。

标签: c# .net


【解决方案1】:

对于一个简单的任务可能有些矫枉过正,但我​​想做一个练习:

ID 类

public class AlphaNumericID
{
    private string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public int Alpha { get; protected set;}
    public int Numeric { get; protected set; }

    public int NumericLenght { get; protected set; }

    public string KeyFront { get; protected set; }
    public string KeyEnd { get; protected set; }

    public AlphaNumericID(string keyFront, string keyEnd, int numericLength)
    {
        Alpha = 0;
        Numeric = 1;

        KeyFront = keyFront;
        KeyEnd = keyEnd;

        NumericLenght = numericLength;
    }

    public void Increment()
    {
        Numeric++;

        if (Numeric == Math.Pow(10, NumericLenght))
        {
            Alpha++;
            Numeric = 1;

            if (Alpha == chars.Length)
                throw new Exception("Overflow!");
        }
    }

    public override string ToString()
    {
        return String.Format("{0}-{1}{2}-{3}", KeyFront, chars[Alpha], Numeric.ToString().PadLeft(NumericLenght, '0'), KeyEnd);
    }
}

你可以这样使用:

  • 声明

    var id = new AlphaNumericID("123", "A12", 8); //Will create 123-A00000001-A12
    

    (尽管如果您知道您的数据库永远不会变得如此之高,您可能需要考虑使用较短的 id)

  • 增量

    id.Increment();
    
  • 输出

    id.ToString();
    

这种封装的好处是您可以轻松扩展和更改其内部实现,尽管它可能对于您的特定需求来说太大了。

【讨论】:

  • 非常感谢你给我完整的代码。
  • 不客气,但是尽管代码很容易使用,您仍然应该查看代码本身并尝试了解发生了什么。这将帮助您学习,也许您会找到自己改进的方法:)
  • 是的,我现在就在做,谢谢
【解决方案2】:

我会使用一个简单的计数器来递增,并使用第二个属性来获取该数字并输出带有您的 ID 格式的格式化字符串。

你这样写:

int Counter= 100; 
string ID=String.Format("123-A{0}-A12",Counter);

【讨论】:

  • 对于 099,你用了什么字符串。将 099 替换为 100?
  • 你不替换。你说 int Counter= 100;和字符串 ID=String.Format("123-A{0}-A12",Counter);
  • 如果您希望您的值显示为 123-A00000010-A12 而不是 123-A10-A12,则可能是 Counter.ToString("00000000")
【解决方案3】:

你这样说:

 private void bntOK_Click(object sender, EventArgs e)
    {
        int cnt10, ntEQ;
        cnt10 = 10;
        string id = null;  
        string dsp = txtInput.Text.Trim(' '); 

        int rs = string.Compare(dsp, "9");
        if (rs == 0)
        {
            id = string.Format("123-000000{00}-A12", cnt10);
            txtResult.Text = id; 
        }

        else 
        {
            int dspt = Convert.ToInt16(txtInput.Text.Trim());
            ntEQ = dspt + 1;
            id = string.Format("123-000000{00}-A12", ntEQ);
            txtResult.Text = id;
        }

    }

【讨论】:

  • 非常感谢,摇滚兄弟
  • @GeorgeofJungle 非常欢迎你,兄弟
猜你喜欢
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 2019-02-27
  • 2014-05-04
  • 2011-05-01
  • 2011-09-25
  • 1970-01-01
相关资源
最近更新 更多