开发者

String handle in C#

I don't understand the use of "get" and "set" command, how to add a list of the accounts using the code below and someone give me some example to fix the error? Thanks! :(

    public class Account
    {

        public string UserName

        {get rerurn textBox1.Test; // error
        set UserName = textBox1.Text;} // error

        public string Password { get; set; }
        public string RePassword { get;开发者_JAVA百科 set; }
        public string Name { get; set; }
        public string bd { get; set; }
        public string dt { get; set; }
        public string dc { get; set; }
    }
    public class ListAcc
    {
        static void Data()
        {
            List<Account> UserList = new List<Account>();
        }
    }

Okey, thanks everybody, i had fixed that error but if code:

 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;
                UserList.Add(acc);
            }
        }

there are a error from access to textBox1.Text? ( An object reference is required for the nonstatic field, method, or property)... Someone can help?


Change UserName method to

 public string UserName
    {

            get { return textBox1.Text;}
            set { textBox1.Text = value;} 
    }


  1. There's no 'textBox1' field in Account class - I think that You copied that from some other code.
  2. spelling error: there should be 'return' not rerurn

    public class Account
    {    
        public string UserName {get; set;}             
        public string Password { get; set; }
        public string RePassword { get; set; }
        public string Name { get; set; }
        public string bd { get; set; }
        public string dt { get; set; }
        public string dc { get; set; }
    }
    public class ListAcc
    {
        static void Data()
        {
            List<Account> UserList = new List<Account>();
            //example of adding user account
            Account acc = new Account();
            acc.UserName ="John Doe";
            UserList.Add(acc);
        }
    }
    


The get and set should be enclosed with curly braces { }, you also typed "rerun" instead of "return", to fix it, change the Username property to:

public string UserName
{
        get 
        {  
             return textBox1.Text; 
        }
        set 
        {  
             textBox1.Text = value;
        } 
}

Note: make sure your class has access to the "textBox1" control


publuc string variableName
{
 get{ return _variableName;}
 set{ _variableName=value;}
}

get and set are getters and setters for the property so when ever you call this property the value returned by return in your get is what you get.

when ever you assign any value to the property it gets assigned to the variable to which you say variableName=value;

  publuc string variableName
    {
     get;
     set;
    }

when ever you dont want to directly expose any variable(private) you can make property of it using varable names in getter and setter, otherwise mere get;set; will do the job for you.


Add in your Form a new Textbox and be sure it is called TextBox1, then your first example will work.

public class Account {

    public string UserName 

    {get rerurn textBox1.Test; // error 
    set UserName = textBox1.Text;} // error 

    public string Password { get; set; } 
    public string RePassword { get; set; } 
    public string Name { get; set; } 
    public string bd { get; set; } 
    public string dt { get; set; } 
    public string dc { get; set; } 
} 
public class ListAcc 
{ 
    static void Data() 
    { 
        List<Account> UserList = new List<Account>(); 
    } 
}  Okey, thanks everybody, i had fixed that error but if code:

public class ListAcc { static void Data() { List UserList = new List(); //example of adding user account Account acc = new Account(); acc.Username = textBox1.Text; UserList.Add(acc); } }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜