tags:

views:

40

answers:

2

Hi.

I am having problems. I have a subclass based on Randy Patterson's fluent interface example here:

Randy Patterson's Fluent Interface Design Page

I am subclassing a User class and need to pass the properties set in it to the below CreateNewUser method within the UserFluentInterface subclass.

public void CreateNewUser()
              {

                    try
                    {
                        User user = (User)_User;

                        MembershipCreateStatus status;
                        MembershipUser memuser = _membership.CreateUser(user, out status);

                    }
                    catch (Exception e)
                    {
                        string message = e.Message;
                    }

                }

and _membership.CreateUser has the signature:

public MembershipUser CreateUser(User user, out MembershipCreateStatus status);

I am getting the error:

Cannot access a non-static member of outer type 'MyProject.Schema.User' via nested type 'MyProject.Schema.User.UserFluentInterface'

I am not sure how resolve this. Code is as follows:

User user = _someAPI.GetUserProfile(txtEmail.Text);

user.Set
  .Email(txtEmail.Text)
  .Password(txtPassword.Text)
  .CreateNewUser();

Now, I have a User class with a subclass:

 public class User : ProfileBase, ISessionMgrEntry
    {
        MyMembershipProvider _membership = null;
        UserFluentInterface _set = null;

        public User()
        {
            _membership = (MyMembershipProvider)Membership.Providers["MyMembershipProvider"];
            _set = new UserFluentInterface(this);
        }

        public UserFluentInterface Set
        {
            get { return _set; }
        }

        #region ProfileBase Members

            [SettingsAllowAnonymous(false), CustomProviderData("Email;string")]
            public string Email { get { return base["Email"] as string; } set { base["Email"] = value; } }

[SettingsAllowAnonymous(false), CustomProviderData("Password;string")]
            public string Password { get { return base["Password"] as string; } set { base["Email"] = value; } }


           public class UserFluentInterface
            {
                private readonly User _User;

                public UserFluentInterface(User User)
                {
                    _User = User;
                }

                public UserFluentInterface Email(string email)
                {
                    _User.Email = email;
                    return this;
                }

                public UserFluentInterface Password(string password)
                {
                    _User.Password = password;
                    return this;
                }

                public void CreateNewUser()
                {

                    try
                    {
                       User user = ???? // HOW DO I ACCESS THE PROPERTIES THAT WERE ORIGINALLY SET AND PASS THEM AS AN OBJECT BELOW?                        

                        MembershipUser memuser = _membership.CreateUser(user);

                    }
                    catch (Exception e)
                    {
                        string message = e.Message;
                    }

                }

            }

       }
A: 

You can just write User user = _User, if you don't need a copy of _User.

Otherwise, make a copy of _User and pass it on.

Weiming
Thanks. Yeah, I tried that but keep gettingthe following error (hence the post):Cannot access a non-static member of outer type 'MyProject.Schema.User' via nested type 'MyProject.Schema.User.UserFluentInterface'Any thoughts?
Code Sherpa
Read this article: http://www.codeproject.com/KB/cs/nested_csclasses.aspxBasically it means you cannot access members of User in UserFluentInterface. However, you can access those members in UserFluentInterface via the `_User` object, like `_User.Email`, not just `Email`.I think you'd better post the full code for `UserFluentInterface` to see where exactly this error occurs.
Weiming
Hi. Thanks again. There isn't much more to the UserFluentInterface class except for a bunch more getter and setters that really have nothing to do with the problem. I have posted a link to where I am getting the code if that is helpful. If you still feel the need for the code dump let me know and I will do it. I have also posted the CreateNewUser class up top as that is where the error is being thrown. it has something to do with the line _membership.CreateNewUser and "_memberhip" is what intellisense complains about. I am really stuck here.. help much appreciated. Thanks again.
Code Sherpa
Using just _User doesnt work, just like in your Password and Email methods? (MembershipUser memuser = _membership.CreateUser(_User);)
s_hewitt
A: 

I think you're going to have to pass both user and membership into your UserFluentInterface:

            public class UserFluentInterface
            {
                private readonly User _User;
                private readonly Membership _Membership;

                public UserFluentInterface(User User, Membership membership)
                {
                    _User = User;
                    _Membership = membership;
                }
            }

and then use _Membership instead of _membership. in your CreateNewUser() method

s_hewitt