Solved

Structure data inside a public class

Posted on 2013-05-11
2
587 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
2 Comments
 
LVL 74

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now