问题描述
大家好,我正在开发一个ASP.net项目,我遇到了一些错误,这可能是一个新手的错误,这是我想做的事情:
你可以看到我有两个回报我应该怎么做我真的想拥有这个随机密码的东西=)
你可以看到我的错误这是一张图片:
Hi guys, im working on a ASP.net project were i have bumped in to some errors this i probablly a rookie misstake here is what im trying to do:
As you can see i have two "returns" how should i do this i really would like to have this random password thing =)
You can see my error here it's a picture:
protected void b1_Click(object sender, EventArgs e)
{
// RANDOM password
{
const string consonnants = "bcdfghjklmnpqrstvwxz";
const string vowels = "aeiouy";
string passwordz = "";
byte[] bytes = new byte[4];
var rnd = new RNGCryptoServiceProvider();
for (int i = 0; i < 3; i++)
{
rnd.GetNonZeroBytes(bytes);
passwordz += consonnants[bytes[0] * bytes[1] % consonnants.Length];
passwordz += vowels[bytes[2] * bytes[3] % vowels.Length];
}
rnd.GetBytes(bytes);
passwordz += (bytes[0] % 10).ToString() + (bytes[1] % 10).ToString();
return passwordz;
}
// Variabler blir vad användaren matar in i formuläret
first = TB1.Text;
last = TB2.Text;
description = TB3.Text;
telephoneNumber = TB4.Text;
username = first.Replace('å', 'a').Replace('Å', 'A').Replace('ä', 'a').Replace('Ä', 'A').Replace('ö', 'o').Replace('Ö', 'O').Replace('ü', 'u').Replace('Ü', 'u').Replace('é', 'e').Replace('è', 'e').Replace(" ", "").ToLower() + last.Replace('å', 'a').Replace('Å', 'A').Replace('ä', 'a').Replace('Ä', 'A').Replace('ö', 'o').Replace('Ö', 'O').Replace('ü', 'u').Replace('Ü', 'u').Replace('é', 'e').Replace('è', 'e').Replace(" ", "").ToLower();
// Connect to LDAP
DirectoryEntry myLdapConnection = createDirectoryEntry();
createUser(myLdapConnection, domain, first, last, description, telephoneNumber, password, username, true);
// Skickar vidare användaren till en ny sida "klart"
Response.Redirect("~/klart.aspx");
}
static int createUser(DirectoryEntry myLdapConnection, String domain, String first,
String last, String description, String telephoneNumber, object[] password,
String username, bool enabled)
{
// create new user object and write into AD
DirectoryEntry user = myLdapConnection.Children.Add(
"CN=" + first + " " + last, "user");
// User name (domain based)
user.Properties["userprincipalname"].Add(username + "@" + domain);
// User name (older systems)
user.Properties["samaccountname"].Add(username);
// Surname
user.Properties["sn"].Add(last);
// Forename
user.Properties["givenname"].Add(first);
// Display name
user.Properties["displayname"].Add(first + " " + last);
// Description
user.Properties["description"].Add(description);
// E-mail
user.Properties["mail"].Add(username + "@" + domain);
// TelePHÅNE
user.Properties["telephoneNumber"].Add(telephoneNumber);
user.CommitChanges();
// set user's password
user.Invoke("SetPassword", password);
// enable account
if (enabled)
user.Invoke("Put", new object[] { "userAccountControl", "512" });
user.CommitChanges();
return 0;
}
推荐答案
protected void b1_Click(object sender, EventArgs e)
{
...
return passwordz;
void方法不能返回任何类型的值。
如果你定义一个返回值的方法,那么:
1)它不能用作处理程序对于一个事件,因为签名不匹配。
2)它必须在所有路径上返回一个值,或者抛出异常。您不能在一个分支上重新调整值,而不能在另一个分支上重新调整:
A void method cannot return a value of any sort.
And if you define a method that returns a value then:
1) It cannot be used as the handler for an event, because the signature will not match.
2) It must either return a value on all paths, or throw an exception. You cannot retun a value on one branch, and not on another:
protected void b1_Click(object sender, EventArgs e)
{
// RANDOM password
{
...
return passwordz;
}
// Variabler blir vad användaren matar in i formuläret
...
Response.Redirect("~/klart.aspx");
}
编译器(非常正确地)不允许这样做。
The compiler will (quite rightly) not allow that.
protected void b1_Click(object sender, EventArgs e)
{
// RANDOM password
const string consonnants = "bcdfghjklmnpqrstvwxz";
const string vowels = "aeiouy";
// avoid the string concatenations
System.Text.StringBuilder passwordz = new System.Text.StringBuilder();
byte[] bytes = new byte[4];
var rnd = new RNGCryptoServiceProvider();
for (int i = 0; i < 3; i++)
{
rnd.GetNonZeroBytes(bytes);
passwordz.Append(consonnants[bytes[0] * bytes[1] % consonnants.Length])
.Append(vowels[bytes[2] * bytes[3] % vowels.Length]);
}
rnd.GetBytes(bytes);
passwordz.Append((bytes[0] % 10).ToString()).Append((bytes[1] % 10).ToString());
string password = passwordz.ToString();
// Variabler blir vad användaren matar in i formuläret
first = TB1.Text;
last = TB2.Text;
description = TB3.Text;
telephoneNumber = TB4.Text;
username = first.Replace('å', 'a').Replace('Å', 'A').Replace('ä', 'a').Replace('Ä', 'A').Replace('ö', 'o').Replace('Ö', 'O').Replace('ü', 'u').Replace('Ü', 'u').Replace('é', 'e').Replace('è', 'e').Replace(" ", "").ToLower() + last.Replace('å', 'a').Replace('Å', 'A').Replace('ä', 'a').Replace('Ä', 'A').Replace('ö', 'o').Replace('Ö', 'O').Replace('ü', 'u').Replace('Ü', 'u').Replace('é', 'e').Replace('è', 'e').Replace(" ", "").ToLower();
// Connect to LDAP
DirectoryEntry myLdapConnection = createDirectoryEntry();
createUser(myLdapConnection, domain, first, last, description, telephoneNumber, password, username, true);
// Skickar vidare användaren till en ny sida "klart"
Response.Redirect("~/klart.aspx");
}
static int createUser(DirectoryEntry myLdapConnection, String domain, String first,
String last, String description, String telephoneNumber, String password,
String username, bool enabled)
{
// create new user object and write into AD
DirectoryEntry user = myLdapConnection.Children.Add(
"CN=" + first + " " + last, "user");
// User name (domain based)
user.Properties["userprincipalname"].Add(username + "@" + domain);
// User name (older systems)
user.Properties["samaccountname"].Add(username);
// Surname
user.Properties["sn"].Add(last);
// Forename
user.Properties["givenname"].Add(first);
// Display name
user.Properties["displayname"].Add(first + " " + last);
// Description
user.Properties["description"].Add(description);
// E-mail
user.Properties["mail"].Add(username + "@" + domain);
// TelePHÅNE
user.Properties["telephoneNumber"].Add(telephoneNumber);
user.CommitChanges(); // Why .CommitChanges() before you're done?
// set user's password
user.Invoke("SetPassword", password);
// Instead of Invoke, why not:
user.Password = password;
// enable account
if (enabled)
// user.Invoke("Put", new object[] { "userAccountControl", "512" }); // new object[]... is unnecessary
user.Invoke("Put", "userAccountControl", "512");
user.CommitChanges();
return 0;
}
这篇关于C#two" return"在同一名称空间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!