C#two" return"在同一名称空间中 [英] C# two "return" in the same namespace
问题描述
大家好,我正在开发一个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;
}
推荐答案
你不能这样做:
You can't do this:
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.
我该怎么做=)?我真的需要这个功能,我可以轻松地重复它,就像这样:
object [] password = {12345678ADWdddw};
然后是user.Invoke(SetPassword,密码);
但这会设置一个静态密码,我会非常像一个随机的。
What should i do then =) ?I really need this function, i could to it reallt easy and to it like this:
object[] password = { "12345678ADWdddw" };
and then user.Invoke("SetPassword", password);
But this will set a static password, i would really like a randomized.
看起来你认为你需要一个返回来从它构建的范围中获取passwordz值。你没有。
你在哪里声明传递给createUser(..的
??object []密码
。)
您不需要将密码作为对象[]
。
阅读params
关键字。这是将可变数量的值传递给方法的方法。
所以,更改createUser(...)
只需要一个字符串密码
参数并直接设置。
It looks like you think you need a return to get the passwordz value out of the scope where it is built. You don't.
Where are you declaring theobject[] password
that you pass to thecreateUser(...)
??
You don't need for the password to be anobject[]
.
Read up on theparams
keyword. That is the way to pass a variable number of values to a method.
So, change thecreateUser(...)
to just take astring password
parameter and set it directly.
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"在同一名称空间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!