开发者

C# console for an auctioning system, getting an error code and really not sure why

I am programming an auctioning system in C# on visual studio 2010, i have 7 classes: auction system bid buyer seller item user,

on my user class i am trying to program my username and password but get an error message, below is my code.

public abstract class user
{
    protected string username;
    protected string password;

    public user(string username, string password)
    {
        this.username = username;
        this.**password** = password;
    }
    public string getusername()
    {
        return username;
    }
    public string password()
    {
        return *开发者_C百科*password**;
    }
}

the errors are in bold (double ** next to the words)

I am extremely novice at C# please help if possible. I am doing tutorials etc so no need to post them just in a predicament where I need this sorting.


The problem is that you have a string field called password and a method that returns a string called password. The compiler doesn't know which you mean.

Try renaming your method to getpassword:

public string getpassword()
{
    return password;
}

There are different ways of coding this - but this should get you started.

NOTE as C# is case sensitive the usual convention is to start methods with upper case and use Pascal Case for compound names. So your methods would become:

public string GetUsername()
{
    return username;
}

public string GetPassword()
{
    return password;
}

had you done this originally then you wouldn't have had a problem as Password for the method name would have been different to password for the field name.

See this page on .NET naming conventions for more information.


You have methods and fields with the same name. Either go with properties or rename your methods. Example code coming up...

public abstract class User
{
    protected string _username;
    protected string _password;

    public user(string username, string password)
    {
        this._username = username;
        this._password = password;
    }

    public string GetUsername()
    {
        return _username;
    }

    public string GetPassword()
    {
        return _password;
    }
}

or

public abstract class User
{
    protected string _username;
    protected string _password;

    public user(string username, string password)
    {
        this._username = username;
        this._password = password;
    }

    public string Username
    {
        get 
        {
            return _username;
        }

        set
        {
             _username = value;
        }
    }

    public string Password
    {
        get
        {
            return _password;
        }
        set
        {
            _password = value;
        }
    }
}


Your error on "password" occurs because "password" is a method and not a property.

try this:

public class User {
  public User (string name, string password) {
    this.Username = username;
    this.Password = password;
  }
  public string Username { get; set; }
  public string Password { get; set; }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜