Search code examples
c#inheritancesubclassmainclass

Main class USER and subclass PLAYER, how to PLAYER=USER ? c#


I'm making the first server-client application and i need your help.
The client must autenticate itself and after it can play some games.
So I've got USER and PLAYER classes: USER comprends all user registered and PLAYER comprends user that play game (there are PLAYER_GAME1,PLAYER_GAME2, etc..).
Inside USER i've proprieties like name, surname, id and etc.
Inside PLAYER i need have the proprieties of the user plus point, time in game, etc.

Actually:

     public class USER
{
        public string name, surname, ID, password, IP;
        WALLET wallet;
        bool login;
        datetime lastAccess;
        TcpClient socket;    
        Thread receiveMessages;//this receive message for log-in
        ...*other 30 proprieties*
     public USER(string n,string s,string _id,string pw)
    {
     *inizialize all variables*
    }
   }

   public class PLAYER
    {
      public USER user;
        Thread receiveMessages;
        int points;
        bool online;
        ...*other proprieties*

     public PLAYER(USER u)
     {
      user=u;
     *inizialize all variables*
     }
    }

so for getting name i have to do:

PLAYER p= new PLAYER(new USER(...));
string name=p.user.name;

I think is more smart make PLAYER subclass of USER and when user want play games i "expand" the class user with the proprieties of player so i need do :

            public class USER
            {
                protected string name, surname, ID, password, IP;
                WALLET wallet;
                bool login;
                datetime lastAccess;
                TcpClient socket;    
                Thread receiveMessages;//this receive message for meneage the game
                ...*other 30 proprieties*

             public USER(string n,string s,string _id,string pw)
            {
             *inizialize all variables*
            }
           }

           public class PLAYER : USER
            {
                Thread receiveMessages;
                ...*other proprieties*

             public PLAYER():base()// here, what could i do?
             {
             *inizialize all PLAYER variables*
             }
            }

so for getting name i would do:

PLAYER p= new PLAYER();
p=exixtingUser;
string name=p.name;

I know that SUBCLASS=MAINCLASS is impossible so how could i do it?


Solution

  • As I can understand, you want to create a Player role based on the User information.

    If our classes are:

    public class User
    {
        public Guid Id { get; private set; }
        public string Name { get; private set; }
    
        public User(Guid id, string name)
        {
            Id = id;
            Name = name;
        }
    }
    
    public class Player : User
    {
        public TimeSpan TimeInGame { get; set; }
    
        public Player(User userInfo)
            : base(userInfo.Id, userInfo.Name)
        {
    
        }
    }
    

    usage of such constructor will be:

    var player = new Player(user);
    

    You can use a constructor accepting the User information. You also can write the extension method .ToPlayer() or something like it.

    I think you should read the article about inheritance on the MSDN and continue the reading with Constructors articles.


    Update:

    I've understood your problem, but, unfortunately, there is no easy solution for it. You can either remain on your current solution, this will be alright if your application creates many players based on one user. If it is a -one-to-one relation, you can do something like this:

    public class User
    {
        public Guid Id { get; private set; }
        public string Name { get; private set; }
    
        public User(User copyFrom)
        {
            Id = copyFrom.Id;
            Name = copyFrom.Name;
            // copy all the fields you need
        }
    }
    
    public class Player : User
    {
        public TimeSpan TimeInGame { get; set; }
    
        public Player(User userInfo)
            : base(userInfo)
        {
    
        }
    }
    

    The main problem of this solution is that you have to copy it by yourself, without any automation mechanism. You can find a similar question here:

    Copying the contents of a base class from a derived class