我已经花了一些时间在这个密码生成器上,但我太蠢了,无法让它工作!
我有一个带有生成的用户名和密码的输入和输出文本框。
问题是第三个文本框,“inputLength”,我希望用户在 GeneratePassword() 中决定密码的长度
private void button1_Click(object sender, EventArgs e)
{
char[] newRow = new char[] { '\r', '\n' };
string[] name = input.Text.Split(newRow, StringSplitOptions.RemoveEmptyEntries);
string result = "";
for (int i = 0; i < name.Length; i++)
{
result += string.Format("{0}{1} # {2}\r\n",name[i].Substring(0, 3), i, GeneratePassword());
}
output.Text = result;
}
private static string GeneratePassword()
{
int passwordLength = Int32.Parse(inputLength.Text);
string result = "";
string allowedChars = "1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
Random random = new Random();
for (int i = 0; i < inputLength; i++)
{
result += allowedChars[random.Next(allowedChars.Length)].ToString();
System.Threading.Thread.Sleep(10);
}
return result;
throw new NotImplementedException();
}
最佳答案
因为GeneratePassword
是静态的,只能访问静态成员和方法。
inputLenght
是一个 TextBox
并且是您表单的成员,因此 GeneratePassword
无法访问它。
简单地从 GeneratePassword
定义中删除 static 或将 inputLength.Text
作为参数传递给 GeneratePassword
函数。
关于c# - 当前上下文中不存在名称 'inputLength',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27404292/