Class Initialization

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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;
}
}
 
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;
}
}
 
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
 
Back
Top