Solved

Structure data inside a public class

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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