private string GenerateID()
{
}
private void auto()
{
AdmissionNo.Text = "A-" + GenerateID();
}
前缀为A,如下所示 A-0001 A-0002等。
答案 0 :(得分:0)
您可以使用以下代码。
`private string GenerateID() {
int lastAddedId = 8;// get this value from database
string demo = Convert.ToString(lastAddedId+1).PadLeft(4, '0');
return demo;
// it will return 0009
}
private void auto()
{
AdmissionNo.Text = "A-" + GenerateID();
// here it will set the text as "A-0009"
}
答案 1 :(得分:0)
看看这个
public class Program
{
private static int _globalSequence;
static void Main(string[] args)
{
_globalSequence = 0;
for (int i = 0; i < 10; i++)
{
Randomize(i);
Console.WriteLine("----------------------------------------->");
}
Console.ReadLine();
}
static void Randomize(int seed)
{
Random r = new Random();
if (_globalSequence == 0) _globalSequence = r.Next();
Console.WriteLine("Random: {0}", _globalSequence);
int localSequence = Interlocked.Increment(ref _globalSequence);
Console.WriteLine("Increment: {0}, Output: {1}", _globalSequence, localSequence);
}
}
答案 2 :(得分:0)
恕我直言,它是否是Windows应用程序都不重要。我宁愿关心线程安全。因此,我将使用以下内容:
public sealed class Sequence
{
private int value = 0;
public Sequence(string prefix)
{
this.Prefix = prefix;
}
public string Prefix { get; }
public int GetNextValue()
{
return System.Threading.Interlocked.Increment(ref this.value);
}
public string GetNextNumber()
{
return $"{this.Prefix}{this.GetNextValue():0000}";
}
}
可以很容易地增强它以使用数字计数。因此,“ 0000”部分也可以动态指定。