TheKingPinMirza
TheKingPinMirza

Reputation: 8912

Error "is property but is used like type"

I am getting error "Model.Password is property but is used like type".

What i am trying to do is basically create class constructor to assign default value to Password property of class NewUser.

I'm quite new to C# so appreciate if i can be guided what i'm doing wrong here and how to correct it?

using System.ComponentModel.DataAnnotations;
namespace Model
{
    public class NewUser
    {
        public string Password { get; set; }
        public class NewUser()
        {
        Password = "Admin123";
        }
    }
}

Upvotes: 1

Views: 3045

Answers (2)

Icemanind
Icemanind

Reputation: 48686

Constructors do not have a return type. Change your code to this:

using System.ComponentModel.DataAnnotations;
namespace Model
{
    public class NewUser
    {
        public string Password { get; set; }
        public NewUser()
        {
            Password = "Admin123";
        }
    }
}

When you define a constructor, you just declare the visibility (public, protected, or private) and then the name of the class (NewUser in this case), then optional parameters. In your case, you don't look like you want any. But you could do this too:

public NewUser(string password)
{
    Password = password;
}

Then instantiate it like this:

var newUser = new NewUser("Admin123");

And its okay to have both constructors, if you want. That way they can implicitly specify a password, or if they don't specify one, default it to something (such as Admin123)

Upvotes: 4

artm
artm

Reputation: 8584

Remove class from the Ctor. You don't return the type from the constructor, you just set construsctor access modifier.

namespace Model
{
    public class NewUser
    {
        public string Password { get; set; }

        public NewUser()
        {
            Password = "Admin123";
        }
    }
}

Upvotes: 1

Related Questions