P
P
Peter2018-05-24 14:33:20
C++ / C#
Peter, 2018-05-24 14:33:20

How to write a class for users?

Good afternoon. Need help understanding how to create a class that will act as a repository for comments from users. This is not a combat application and never will be. Rather, for a general understanding of how it works.

There is such a User class:
public class User
    {
        public int UserID;
        public string NickName;
        public string HomeTown;
        public string BirthDate;
        public string DateOfRegistration;
        public UserRole Role;

        public User(int userid,string nickname,string hometown,string birthdate, string date_of_registration, UserRole role)
        {
            this.UserID = userid;
            this.NickName = nickname;
            this.HomeTown = hometown;
            this.BirthDate = birthdate;
            this.DateOfRegistration = date_of_registration;
            this.Role = role;
        }

        public override string ToString()
        {
            return $"UserID --> {UserID}\nNickName --> {NickName}\nNomeTown --> {HomeTown}\nBirthDate --> {BirthDate}\nDateOfRegistration --> {DateOfRegistration}\nUserRole --> {Role}";
        }
    }

This is the enum for UserRole:
public enum UserRole : byte
    {
        Administrator,
        Moderator,
        DefaultUser,
    }

And there is a class for the comments themselves:
public class UserContent
    {
        public int ContentID;
        public string Content;
        public User Author;
        public string ContentTime;

        public UserContent(int content_id, string content, User author, string content_time)
        {
            this.ContentID = content_id;
            this.Content = content;
            this.Author = author;
            this.ContentTime = content_time;
        }

        public override string ToString()
        {
            return $"ID Статьи --> {ContentID}\nСтатья --> {Content}\nАвтор статьи --> {Author.NickName}\nДата создания --> {ContentTime}";
        }

    }


The difficulty is how to pass a field with the user's nickname to the comment class? The second constructor for the user will not work. Since when initializing the class, you need to manually prescribe a nickname. And it is necessary that the nickname is taken from the field of the User class.
This is how it looks in Program.cs:
static void Main(string[] args)
        {
            
            List<User> users = new List<User>();
            users.Add(new User(1,"Morpheus","Zion","Dec 15,1985","Jan 12, 2010",UserRole.Administrator));
            users.Add(new User(2,"Trinity","Zion","May 10,1980","Sep 6, 2012", UserRole.Administrator));
            users.Add(new User(3,"Apok","Zion","Mar 22,1970" , "Feb 11, 2011", UserRole.Moderator));
            users.Add(new User(4,"Neo", "Los-Angeles","Jul 2,1982", "Dec 7,2012", UserRole.DefaultUser));

            List<UserContent> content = new List<UserContent>();
            content.Add(new UserContent(1, "Hello Zion!", new User("Morpheus"), "May 24,2018"));

            Console.ReadKey();
        }

The question is in this line:
content.Add(new UserContent(1, "Hello Zion!", new User("Morpheus"), "May 24,2018"));

Instead of new User, I would just like to pass user.NickName.
Is it possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
heartdevil, 2018-05-24
@Morpheus_God

var theUser = new User("Morpheus");
content.Add(new UserContent(1, "Hello Zion!", theUser, "May 24,2018"));

Another thing is that you need to dress up something unique, and not NickName. To uniquely identify the user. By the way, this should be done almost everywhere where there are links like User-Comments and others.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question