如何在变量中获取方法生成的值,可以在按钮单击时访问该变量 [英] How do I get the value generated by a method in a variable which can be accessible on button click
本文介绍了如何在变量中获取方法生成的值,可以在按钮单击时访问该变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图生成随机数n我正在使用winform c#代码。我想一次发送生成的随机数到数据库和我的邮件。问题是每次上次生成的随机数被连接。但是这不应该发生我如何限制它。
我尝试过:
public static string strrandom = string.Empty;
void Generate_OTP()
{
}
解决方案
最简单的解决方案是返回值,而不是使用全局变量:
private static Random objran = new Random();
私有静态字符串Generate_OTP()
{
string strRandom =;
string chars =0123456789;
int No_of_Characters = 6;
for(int i = 0; i< No_of_Characters; i ++)
{
int pos = objran.Next(1,chars.Length);
string newChar = chars [pos] .ToString();
strRandom + = newChar;
chars = chars.Replace(newChar,);
}
返回strRandom;
}
然后你就这样使用它:
SQL.Parameters.AddWithValue(TEXT_ME_RESET_PASSWORD,Generate_OTP( ));
i tried to generate random numbers n i was using winform c# code . i want to send generated random number at a time to database and my mail.here the problem is every time previous generated random num is getting concatenated .but this should not happen how do i restrict it.
What I have tried:
public static string strrandom = string.Empty; void Generate_OTP() { }
解决方案
Simplest solution is to return the value, instead of using a "global variable":
private static Random objran = new Random(); private static string Generate_OTP() { string strRandom = ""; string chars = "0123456789"; int No_of_Characters = 6; for (int i = 0; i < No_of_Characters; i++) { int pos = objran.Next(1, chars.Length); string newChar = chars[pos].ToString(); strRandom += newChar; chars = chars.Replace(newChar, ""); } return strRandom; }You then just use it like this:
SQL.Parameters.AddWithValue("TEXT_ME_RESET_PASSWORD", Generate_OTP());
这篇关于如何在变量中获取方法生成的值,可以在按钮单击时访问该变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文