Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文本框添加到列表中!c# 公共类帐户 { 公共字符串用户名 { 获取{返回用户名;} 设置{Username=value;} } } 公共类列表ACC { 静态void数据() { List UserList=新列表(); //添加用户帐户的示例 账户acc=新账户(); acc.Username=textBox1.Text;//错误 UserList.Add(acc); } }_C# - Fatal编程技术网

将文本框添加到列表中!c# 公共类帐户 { 公共字符串用户名 { 获取{返回用户名;} 设置{Username=value;} } } 公共类列表ACC { 静态void数据() { List UserList=新列表(); //添加用户帐户的示例 账户acc=新账户(); acc.Username=textBox1.Text;//错误 UserList.Add(acc); } }

将文本框添加到列表中!c# 公共类帐户 { 公共字符串用户名 { 获取{返回用户名;} 设置{Username=value;} } } 公共类列表ACC { 静态void数据() { List UserList=新列表(); //添加用户帐户的示例 账户acc=新账户(); acc.Username=textBox1.Text;//错误 UserList.Add(acc); } },c#,C#,访问textBox1时出错。文本?(非静态字段、方法或属性需要对象引用)。。。有人能帮忙吗 但如果代码是: public class Account { public string Username { get { return Username; } set { Username = value; } } } public

访问textBox1时出错。文本?(非静态字段、方法或属性需要对象引用)。。。有人能帮忙吗

但如果代码是:

 public class Account
        {
            public string Username
            {
                get { return Username; }
                set { Username = value; }
            }
        }


public class ListAcc
        {
            static void Data()
            {
                List<Account> UserList = new List<Account>();
                //example of adding user account
                Account acc = new Account();
                acc.Username = textBox1.Text; //error
                UserList.Add(acc);
            }
        }
private void textBox1\u TextChanged(对象发送者,事件参数e)
{
List UserList=新列表();
//添加用户帐户的示例
账户acc=新账户();
acc.Username=textBox1.Text;
UserList.Add(acc);
}

这是工作!有人能帮我纠正错误吗?非常感谢

TextBox1是无法在静态方法中访问的成员变量。 您可以使用下面的代码

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
            List<Account> UserList = new List<Account>();
            //example of adding user account
            Account acc = new Account();
            acc.Username = textBox1.Text;
            UserList.Add(acc);
    }
公共类列表ACC
{
私有静态列表用户列表;
公共静态列表数据()
{ 
返回用户列表;
} 
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{ 
UserList=新列表();
//添加用户帐户的示例
账户acc=新账户();
acc.Username=textBox1.Text;
UserList.Add(acc);
} 

TextBox1是一个无法在静态方法中访问的成员变量。 您可以使用下面的代码

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
            List<Account> UserList = new List<Account>();
            //example of adding user account
            Account acc = new Account();
            acc.Username = textBox1.Text;
            UserList.Add(acc);
    }
公共类列表ACC
{
私有静态列表用户列表;
公共静态列表数据()
{ 
返回用户列表;
} 
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{ 
UserList=新列表();
//添加用户帐户的示例
账户acc=新账户();
acc.Username=textBox1.Text;
UserList.Add(acc);
} 

请注意,
数据
方法是静态的,
textBox1\u TextChanged
不是静态的
textBox1
是一个实例变量,它属于类的特定实例<代码>静态方法属于类本身,无法查看实例变量<代码>静态方法不知道与哪个实例对话


为什么希望
Data
方法是
static

请注意
Data
方法是静态的,而
textBox1\u TextChanged
不是
textBox1
是一个实例变量,它属于类的特定实例<代码>静态方法属于类本身,无法查看实例变量<代码>静态方法不知道与哪个实例对话


为什么希望
数据
方法是
静态

如果不使用对对象(例如form1.textBox1)的引用,就无法从静态方法访问对象的字段/属性/方法(在本例中,来自容器对象(例如WinForm对象)的textBox1)。TextBox1在容器对象(例如form1)内可能也是私有的,因此您也无法在ListAcc对象的form1对象外访问它

下面是一些应该可以工作的示例代码

public class ListAcc 
{
            private static List<Account> UserList;
            public static List<Account> Data() 
            { 
                 return UserList;
            } 
        }
private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
            UserList = new List<Account>(); 
            //example of adding user account 
            Account acc = new Account(); 
            acc.Username = textBox1.Text; 
            UserList.Add(acc); 
    } 
公共类列表ACC
{
//不要将其放入数据中,否则您将重新创建它
//再次调用数据时
私有静态列表UserList=新列表();
//我们将通过name参数而不是
//我们不能直接引用它
公共静态无效数据(字符串名称)
{
//添加用户帐户的示例
账户acc=新账户();
acc.用户名=名称;
UserList.Add(acc);
}
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
//我们将调用数据并传入textBox1.Text作为用户名
列表附件数据(textBox1.Text);
}

请注意,TextChanged事件并不是放置数据方法的最佳位置,因为每次输入新字符/按backspace/等时都会引发该事件。

您无法访问对象的字段/属性/方法(在本例中,textBox1来自容器对象,例如WinForm对象)不使用对象引用的静态方法,例如form1.TextBox1。TextBox1在容器对象(例如form1)内可能也是私有的,因此您也无法在ListAcc对象的form1对象外访问它

下面是一些应该可以工作的示例代码

public class ListAcc 
{
            private static List<Account> UserList;
            public static List<Account> Data() 
            { 
                 return UserList;
            } 
        }
private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
            UserList = new List<Account>(); 
            //example of adding user account 
            Account acc = new Account(); 
            acc.Username = textBox1.Text; 
            UserList.Add(acc); 
    } 
公共类列表ACC
{
//不要将其放入数据中,否则您将重新创建它
//再次调用数据时
私有静态列表UserList=新列表();
//我们将通过name参数而不是
//我们不能直接引用它
公共静态无效数据(字符串名称)
{
//添加用户帐户的示例
账户acc=新账户();
acc.用户名=名称;
UserList.Add(acc);
}
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
//我们将调用数据并传入textBox1.Text作为用户名
列表附件数据(textBox1.Text);
}

请注意,TextChanged事件并不是放置数据方法的最佳位置,因为每次输入新字符/按backspace/等时都会引发它。

textBox1是属于对象的对象变量,因此在使用它之前需要创建ListAcc类的对象(实例)

void Data()是属于而不是对象的静态方法,这意味着您可以在不创建其实例的情况下使用它们