Solved

Structure data inside a public class

Posted on 2013-05-11
2
630 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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