Solved

Structure data inside a public class

Posted on 2013-05-11
2
635 Views
Last Modified: 2013-05-11
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

0
Comment
Question by:webressurs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39158083
You're attempting to use nested classes/structs. This only defines a class/struct; it does not actually expose any properties of this type. If you maintain this structure, then you need to add another property on your User class that is of type Interests.

e.g.

public class User
    {
        public string UserName { get; set; }
        public string BirthDate { get; set; }
        public Interests UserInterests { get; set; }

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

    }

Open in new window


Then in your controller code you can assign to this new property:

...

            if (objUser != null)
            {
                return new API.Models.User
                {
                    UserName = objUser.UserName,
                    BirthDate = p["BirthDate"].ToString(),
                    UserInterests = new User.Interests()  // "User" is required here if you make "Interests" a nested class
                    {
                        Music = p["Interests.Music"].ToString(),
                        Sport = p["Interests.Sport"].ToString(),
                        Books = p["Interests.Books"].ToString()
                    }
                };
            }

...

Open in new window


I think it will be easier for you if you break this class out into its own separate file, however. There's nothing technically wrong with a nestec class/struct, but they can be difficult to locate and use for those unfamiliar with them.
0
 
LVL 1

Author Closing Comment

by:webressurs
ID: 39158137
Perfect, thank you so much :)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question