1

I want to read data from db and save data in class of user. But this class has only UserId and UserName. I want to save another information.what should I do. This is my code:

User user = new User();
user.UserId = (int)ddr["id"];
user.UserName = (string)ddr["name"];
Maryam
  • 41
  • 1
  • 11

1 Answers1

1

Inheritance through composition example:

public class UserInfo
    {
        private User User { get; set; }

        public int UserId
        {
            get { return User.UserId; }
            set { User.UserId = value; }
        }

        public string UserName
        {
            get { return User.UserName; }
            set { User.UserName = value; }
        }

        public string Email { get; set; }



        public UserInfo()
        {
            User = new User();
        }
    }

So you can use the new UserInfo as User.

UserInfo user = new UserInfo();
user.UserId = (int)ddr["id"];
user.UserName = (string)ddr["name"];
user.Email = (string)ddr["email"];
shadow
  • 1,883
  • 1
  • 16
  • 24
  • ok. now how can I use the object of class in other pages of my site? – Maryam May 10 '16 at 18:17
  • As you would use a `User` object. – shadow May 10 '16 at 18:25
  • :) . It's unknown in other pages. I can't write textbox1.text=user.Email. – Maryam May 10 '16 at 18:27
  • @Maryam create an object on that page too `UserInfo user = new UserInfo();` and then you'll have the access `textbox1.text=user.Email` – Raktim Biswas May 10 '16 at 18:32
  • I'm afraid that I can't help you from here. It's all about the design of your application. For sure there must be some changes so the new class can be used. – shadow May 10 '16 at 18:34
  • the UserInfo class is unknown in other pages. and if I write your code the Email hasn't the value that I given in previous page. It has a new value. Doesn't it?! – Maryam May 10 '16 at 18:38
  • It is a bit confusing for me because I don't know if we are talking about MVC, Web Forms, a Layered application..... if you have controllers, repositories, .... For start I would change in the UI (in just one page) the `User` with `UserInfo` and take it from there. I'm very sorry but I can't help you more behind a keyboard. – shadow May 10 '16 at 18:46