本文介绍了在文本框中自动生成号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从1开始自动生成文本框中的ID?
如果我们在另一个文本框中输入产品名称,则产品ID为1.
然后添加另一个产品名称,然后autoallall id赋值为2....

how to auto genrate ID in textbox start from 1?
if we write the product name in another textbox then product id is 1.
and i add another product name then automaticall id asign is 2....

推荐答案

internal uint Id {
   get { return id++; }
}
private static id = 1;



当心:此代码不是线程安全的.如果需要从不同的线程访问Id,请确保使用lock:
进行操作. http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx [ ^ ].

现在,使用静态方法需要格外小心,最好避免使用.如果声明的类每次运行仅具有一个实例,则此字段可能不是静态的.如果不必一定是 singleton ,最好不要在一个地方使用此Id,但是 singleton模式是最典型的通用名称方法.请参阅:
http://en.wikipedia.org/wiki/Singleton_pattern [ ^ ].

另请参见:
http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial [^ ].

我看到了许多糟糕的单例实现.对于C#,这是一个不错的选择:
http://csharpindepth.com/Articles/General/Singleton.aspx [ ^ ].

—SA



Beware: this code is not thread safe. If you need to access Id from different threads, make sure you do it with lock:
http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

Now, working with static needs extra care and better be avoided. This field may not be static if the declared class has only one instance per run time, guaranteed. If does not have to be a singleton, and it better should not, if you use this Id only in one place, but the singleton pattern is the most typical universal approach to that. Please see:
http://en.wikipedia.org/wiki/Singleton_pattern[^].

See also:
http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial[^].

I saw many bad singleton implementations. This is a good one, for C#:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

—SA



这篇关于在文本框中自动生成号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:57