0

エラーが発生しました。変数(failturetextおよびuserName)を宣言しているにもかかわらず、エラーは引き続き表示されます。誰か助けてくれませんか?

- Use of Unassigned local variable "FailureText" 
- Use of Unassigned local variable "UserName"


protected void Login1_LoginError(object sender, System.EventArgs e)
{
    TextBox FailureText;
    TextBox UserName; 

    //There was a problem logging in the user
    //See if this user exists in the database
    MembershipUser userInfo = Membership.GetUser(UserName.Text); // errors appear here
    if (userInfo == null)
    {
        //The user entered an invalid username...
        //error appear here ( failuretext.text) 
        FailureText.Text = "There is no user in the database with the username " + UserName.Text;  
    }

ありがとう (:

4

3 に答える 3

3

変数を宣言しましたが、それらのプロパティを参照する前に変数に何も割り当てていません。それがメッセージの原点です。ただし、これらは UI コントロールを表し、関数内でローカル変数にする必要はほとんどないため、より大きな問題があります。それらはおそらく、ローカルで宣言するのではなく、ページ (またはフォーム) に属している必要があります。

于 2012-06-24T16:28:38.280 に答える
0

現在と同じ規則を維持したい場合は、コンパイルが失敗する問題を解決するために、2つの変数を参照する前に、まず2つの変数にnull値を割り当てます。

そのようです:

TextBox FailureText = null;
TextBox UserName = null;

これを行う場合、これらのオブジェクトのプロパティを割り当てようとするときに、それらがまだnullであるかどうかを確認する必要がある場合があります。

上記の人々が述べたように、UIコントロールは常にフォーム自体に対してグローバルである必要があるため、この方法で行うべきではありません。

于 2012-06-24T17:03:53.223 に答える
0

コンパイラメッセージについて - このリンクを見てください

 TextBox FailureText; - you declared a variable, but it is not initialized. you might set it to null / a value / default() - but you must initialize it
 TextBox UserName; - the same

UserName.Text を呼び出すと、コンパイラは初期化されていないことを警告しますUserName

于 2012-06-24T16:29:34.693 に答える