Link to home
Start Free TrialLog in
Avatar of webressurs
webressursFlag for Norway

asked on

Structure data inside a public class

I'm making an ASP.NET Web API. In the User Model class there is a lot of properties that describes the user (like username, birthdate etc). To make it more readable I would like to group or structure properties that belongs together.

As you see in the code below I tried to use "struct" to group Interests, but this does not work. I have also tried to make a class called "Interests" inside the user class, but this does not work either.

The question is, how can I group data (interests) inside a class?

namespace API.Models
{
    public class User
    {
        public string UserName { get; set; }
        public string BirthDate { get; set; }

        public struct Interests
        {
            public byte Music { get; set; }
            public byte Sport { get; set; }
            public byte Books { get; set; }
        }

    }
}


namespace API.Controllers.User
{

    [System.Web.Http.Authorize]
    public class UserController : ApiController
    {

        public API.Models.User GetUser(string userName)
        {
            var mp = Membership.Providers[ApplicationName.ToLower()];
            var objUser = mp.GetUser(userName, true);
            var p = System.Web.Profile.ProfileBase.Create(userName, true);
            
            if (objUser != null)
            {
                return new API.Models.User
                {
                    UserName = objUser.UserName,
                    BirthDate = p["BirthDate"].ToString(),
                    Interests.Music = p["Interests.Music"].ToString(),
                    Interests.Sport = p["Interests.Sport"].ToString(),
                    Interests.Books = p["Interests.Books"].ToString()


                };
            }
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }

    }

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of webressurs

ASKER

Perfect, thank you so much :)