none
use variables from a class in another class RRS feed

  • Question

  • Hi,

    I create a class that I define some variables with their properties.

    also I have two class "Form1" and "Form2".

    I assign values to this variables in "Form1" but when I want to use the values in "Form2" after I assigned them and show them through MessageBox.Show(), I find out the variables are empty.

    here the codes:

    the variables"

    class Property
        {
            private string a_username;
            private string a_email;
            
    
            public string username
            {
                get { return a_username; }
                set { a_username = value; }
            }
            public string email
            {
                get { return a_email; }
                set { a_email = value; }
            }
    
            public string password { get; set; } = "88306540";
        }

    the assignment: (this function is in "Form1")

    Property pro = new Property();
    
    private void CreateUserInform()
            {
                userid = File.ReadLines(filePath).Skip(idx).Take(1).First();
               // MessageBox.Show(userid);
                HtmlElementCollection elemcol = webBrowser2.Document.GetElementsByTagName("option");
                int i = 0;
                string[] mailservices = new string[elemcol.Count];
                foreach (HtmlElement elem in elemcol)
                {
                    mailservices[i] = elem.InnerText;
                    i += 1;
                }
                pro.username = userid;
                Random rand = new Random();
                mailservice = mailservices[rand.Next(10)];
                pro.email = pro.username + mailservice;
                wb2func_create_mail();
            }

    call function: (this function is in "Form2" and it called after previous function.)

    Property pro = new Property();

    public void signup_fill() { HtmlElementCollection elemcol = site.Document.GetElementsByTagName("input"); foreach (HtmlElement elem in elemcol) { if (elem.Name == "login") elem.SetAttribute("value", pro.username); if (elem.Name == "remail") elem.SetAttribute("value", pro.email); if (elem.Name == "password") elem.SetAttribute("value", pro.password); if (elem.Name == "password2") elem.SetAttribute("value", pro.password); } MessageBox.Show(pro.username); }


    I should mention that the "password" variable was shown pretty good but the others were shown empty.

    also when I call them in "Form1" that I used to define them, it works just fine and shows the correct assignment.



    Friday, August 9, 2019 9:02 AM

Answers

  • Hello,

    You have two different instances of your Property class.

    You make value assignment to the one in first form and work with another (empty) in second form. 

    You need to use the same instance of Property class in both places. 


    Sincerely, Highly skilled coding monkey.

    Friday, August 9, 2019 9:15 AM
  • Hi Hossein Shahbodaghkhan, 

    Thank you for posting here.

    For your question, you want to use the instance created in ‘Form1’ in ‘Form2’.

    I make a sample on my side, and you can refer or modify it.

    Here’s the code.

    Form1:

            public Form1()
            {
                InitializeComponent();
            }
            public Property pro;
            public Form1(Property pro)
            {
                this.pro = pro;
            }
            public void CreateUserInform()
            {
                pro.username = "A";
                pro.email = pro.username +"@...com";
            }
            public void showForm2()
            {
                Form2 form2 = new Form2(pro);
                form2.signup_fill();
            }

    Form2:

            public Property p;
            public Form2()
            {
                InitializeComponent();
            }
            public Form2(Property p)
            {
                this.p = p;
            }
            public void signup_fill()
            {
                MessageBox.Show($"username: {p.username} email: {p.email}");
            }

    Main function:

            static void Main()
            {
                Property p = new Property();
                Form1 form1 = new Form1(p);
                form1.CreateUserInform();
                form1.showForm2();
    
                Application.Run(new Form1());
            }

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, August 12, 2019 3:17 AM
    Moderator

All replies

  • Hello,

    You have two different instances of your Property class.

    You make value assignment to the one in first form and work with another (empty) in second form. 

    You need to use the same instance of Property class in both places. 


    Sincerely, Highly skilled coding monkey.

    Friday, August 9, 2019 9:15 AM
  • Thank you.

    how can I use the instance that I create in "Form1", in "Form2"?

    Friday, August 9, 2019 9:36 AM
  • >how can I use the instance that I create in "Form1", in "Form2"?

    Day or two ago this question was explained in detail in different topic. Use Search over site to find a details.


    Sincerely, Highly skilled coding monkey.

    Friday, August 9, 2019 9:42 AM
  • Thanks.

    I also have a big problem using Process class.

    here is the link:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7424ed99-6e9e-406d-b628-6b4fe33862ee/the-process-not-work-in-c-app?forum=csharpgeneral

    Friday, August 9, 2019 10:00 AM
  • There are a lot of threads like this.

    Please give me a quick answer. thank you.

    Friday, August 9, 2019 10:04 AM
  • Hello,

    Try using the following singleton class.

    public sealed class UserDetails
    {
        private UserDetails()
        {
        }
    
        public string Name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        private static readonly Lazy<UserDetails> Lazy = 
            new Lazy<UserDetails>(() => new UserDetails());
        public static UserDetails Instance => Lazy.Value;
    }

    In one class or form

    UserDetails.Instance.Name = "Karen";

    In the same class, another class or another form.

    var UserName = UserDetails.Instance.Name;


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, August 9, 2019 11:07 AM
    Moderator
  • > Please give me a quick answer.

    To start working on the level on which used Process class you need few years of training and few years of professional work. 

    By my opinion any explanation at your current level of experience will just delay study which you must do if you want to do programming.

    Please find how to refer to property in another object or use Singleton as Karen offer.


    Sincerely, Highly skilled coding monkey.

    Friday, August 9, 2019 2:28 PM
  • The target area of this exercise is to pass parameters into constructor/method and possibly return some value from method. Very basic. Singleton is far too complex for TS level.

    Sincerely, Highly skilled coding monkey.

    Friday, August 9, 2019 2:37 PM
  • Hi Hossein Shahbodaghkhan, 

    Thank you for posting here.

    For your question, you want to use the instance created in ‘Form1’ in ‘Form2’.

    I make a sample on my side, and you can refer or modify it.

    Here’s the code.

    Form1:

            public Form1()
            {
                InitializeComponent();
            }
            public Property pro;
            public Form1(Property pro)
            {
                this.pro = pro;
            }
            public void CreateUserInform()
            {
                pro.username = "A";
                pro.email = pro.username +"@...com";
            }
            public void showForm2()
            {
                Form2 form2 = new Form2(pro);
                form2.signup_fill();
            }

    Form2:

            public Property p;
            public Form2()
            {
                InitializeComponent();
            }
            public Form2(Property p)
            {
                this.p = p;
            }
            public void signup_fill()
            {
                MessageBox.Show($"username: {p.username} email: {p.email}");
            }

    Main function:

            static void Main()
            {
                Property p = new Property();
                Form1 form1 = new Form1(p);
                form1.CreateUserInform();
                form1.showForm2();
    
                Application.Run(new Form1());
            }

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, August 12, 2019 3:17 AM
    Moderator