Class Initialization

S

shapper

Hello,

I have a class as follows:

public class UserPaper {
public MembershipUser user { get; set; }
public string Password { get; set; }

public UserLeaf(MembershipUser newUser) {
UserLeaf(newUser, null);
}
public UserLeaf(MembershipUser newUser, string password) {
this user = newUser;
this password = password;
}
}

I simplified my code but basically I get the following error:
'UserPaper' is a 'type' but is used like a 'variable'

Do I need to repeat the definition in all methods? I am a little bit
confused.

Thanks,
Miguel
 
M

Martin Honnen

shapper said:
Hello,

I have a class as follows:

public class UserPaper {
public MembershipUser user { get; set; }
public string Password { get; set; }

public UserLeaf(MembershipUser newUser) {
UserLeaf(newUser, null);
}
public UserLeaf(MembershipUser newUser, string password) {
this user = newUser;
this password = password;
}
}

I simplified my code but basically I get the following error:
'UserPaper' is a 'type' but is used like a 'variable'

Do I need to repeat the definition in all methods? I am a little bit
confused.

I think you want the following:

public class UserPaper {
public MembershipUser User { get; set; }
public string Password { get; set; }

public UserPaper(MembershipUser newUser): this(newUser, null){

}
public UserPaper(MembershipUser newUser, string newPassword) {
User = newUser;
Password = newPassword;
}
}
 
C

Clive Dixon

Your class is called "UserPaper" but there are constructor-like members
called "UserLeaf". Constructors must have the same name as the class.

Secondly, you need to chain the first constructor onto the second as
follows:

public class UserPaper
{
// snip

public UserPaper(MembershipUser newUser) : this(newUser, null)
{
}

public UserPaper(MembershipUser newUser, string password)
{
this.user = newUser;
this.password = password;
}
}
 
S

shapper

Your class is called "UserPaper" but there are constructor-like members
called "UserLeaf". Constructors must have the same name as the class.

Secondly, you need to chain the first constructor onto the second as
follows:

public class UserPaper
{
    // snip

    public UserPaper(MembershipUser newUser) : this(newUser, null)
    {
    }

    public UserPaper(MembershipUser newUser, string password)
    {
      this.user = newUser;
      this.password = password;
    }

}

The UserPaper and UserLeaf was a mistake when copying the code here.
For sake of simplicity I didn't copy paste the entire code and I wrote
it wrong.

But yes your answers were what I was looking for.

Thank You!
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Inheritance from MembershipUser class 1
Casting 1
user login problems 1
OOP question 2
Simple question 4
Create new user without login 2
Confusion on nothing 2
membership/roles create user problem 3

Top