-1

I am new to C# and I have the following question/s:

DBOps.cs:

class DBOps()
{
internal static void Update(DataGridView dataGridView1)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                var user = new User();
                user.Id = (int) dataGridView1.SelectedRows[0].Cells[0].Value;
                user.Username = (string) dataGridView1.SelectedRows[0].Cells[1].Value;
                user.FirstName = (string) dataGridView1.SelectedRows[0].Cells[2].Value;
                user.LastName = (string) dataGridView1.SelectedRows[0].Cells[3].Value;
                user.Email = (string) dataGridView1.SelectedRows[0].Cells[4].Value;
                user.Password = (string) dataGridView1.SelectedRows[0].Cells[5].Value;

                var updateForm = new UpdateForm(user);
                var result = updateForm.ShowDialog();

                if (result == DialogResult.OK)
                {

                }
                else if (result == DialogResult.Cancel)
                {

                }
            }
        }
}

User.cs:

class User
    {
        public int Id { get; set; }
        public string Username { get; set; }    
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
    }

UpdateForm.cs:

public partial class UpdateForm : Form
    {
        public UpdateForm(User user)
        {
            InitializeComponent();

            txtId.Text = user.Id.ToString();
            txtUser.Text = user.Username;
            txtFName.Text = user.FirstName;
            txtLName.Text = user.LastName;
            txtEmail.Text = user.Email;
            txtPass.Text = user.Password;
        }
//code
}

The values in DBOps class are stored correctly, I've checked that. Now I want to set the TextBoxes in UpdateForm with the values from User.cs.

MrSilent
  • 564
  • 10
  • 28

3 Answers3

1

You just get a reference to the User class and then access them via their property getters. For example:

User user = /* get reference to User */

textbox.Text = user.Username;
Jeff Prince
  • 658
  • 6
  • 13
1

I don't know what you trying to do but to pass an object to another class you can use the constructor method.

public class SecondObject
{
    public SecondObject(User theuser)
    {
        textbox1.Text = theuser.Id
        textbox2.text = theuser.Username
        // ...
    }
}

To use your second class just do :

var user = new User();

user.Id = (int) dataGridView1.SelectedRows[0].Cells[0].Value;
user.Username = (string) dataGridView1.SelectedRows[0].Cells[1].Value;
user.FirstName = (string) dataGridView1.SelectedRows[0].Cells[2].Value;
user.LastName = (string) dataGridView1.SelectedRows[0].Cells[3].Value;
user.Email = (string) dataGridView1.SelectedRows[0].Cells[4].Value;
user.Password = (string) dataGridView1.SelectedRows[0].Cells[5].Value;
SecondObject obj = new SecondObject(user);
Nicolas HENAUX
  • 1,656
  • 1
  • 14
  • 18
1

First of All, I suggest you to use Convert.ToInt32(), Convert.ToString() instead of casting like that in your question.

then please check the accessibility of the class User

You may check the class definition easily by pressing {F12} on your keyboard if you are using Visual Studio.

If the classes are not in the same project, please add a reference.

If the classes are not in the same namespace, please add a line using xxxxx;

If you still cannot get the values, please change all the properties to public.

Finally, I think you can get the values by putting these lines of code:

txtUserID.Text = User.Id.ToString();
txtUserName.Text = User.UserName;
txtFirstName.Text = User.FirstName;
txtLastName.Text = User.LastName;
txtEmail.Text = User.Email;
txtPassword.Text = User.Password;

I don't suggest you to show password in a textbox btw.

Please let me know if you still have any questions about C#

Tony Wu
  • 1,040
  • 18
  • 28
  • Perfect explanation for a newbie in this domain, thank you, it is clear now and it is working as it should. – MrSilent Sep 07 '15 at 17:06
  • Though, for learning purposes, could you please tell me the difference of usage between my casts and your conversions? – MrSilent Sep 07 '15 at 17:06
  • Hi @AlexandruMitu , please read this article from stackoverflow: http://stackoverflow.com/a/1608828/2884831 – Tony Wu Sep 07 '15 at 17:09
  • Thank you so much, @Tony Wu. – MrSilent Sep 07 '15 at 17:10
  • Properties can be accessed with internal modifier with class instance within namespace( e.g objUser.ID), and if it public it can be accessed outside the namespace – Sohail Anjum Sep 10 '15 at 13:02