Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:55:11+00:00 2026-05-29T08:55:11+00:00

I am new in C# programming, but I know that attributes should be encapsulated

  • 0

I am new in C# programming, but I know that attributes should be encapsulated in the class. Therefore I used private declaration

private string _servername;

Problem is that I cannot update _servername from 2nd Form.
I clicked on ChangeName button. New Form2 was appeared. I clicked on button NewName. Methods from Form1 were finished successfully and MessageBox in method _Server2 shown me that value of _servername should be “TEST”
When I click on button CurrentServerName, value of _servername is still “localhost”. Could you please tell me why? How to fix it? Thank you in advance

FORM 1

namespace Test
{
    public partial class Form1 : Form
    {
        private string _servername;
        public Form1()
        {
            InitializeComponent();
            _servername = "localhost";
        }

        public string _Server(string _server)
        {
            _servername = _server;
            string serverlist2 = _server;            
            _Server2(serverlist2);
            return serverlist2;
        }

        public void _Server2(string _server2)
        {
            _servername = _server2;
            MessageBox.Show(_servername);
        }

        private void ChangeName_Click(object sender, EventArgs e)
        {
            Form2 _Form2 = new Form2();
            _Form2.Show();
        }

        private void CurrentServerName_Click(object sender, EventArgs e)
        {
            MessageBox.Show(_servername);
        }

    }
}

FORM 2

namespace Test
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void NewName_Click(object sender, EventArgs e)
        {
            Form1 _Form1 = new Form1();
            _Form1._Server("TEST");
            this.Close();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:55:12+00:00Added an answer on May 29, 2026 at 8:55 am

    You don’t want to create a new instance of Form1 inside Form2. Instead, when you create an instance of Form2 to show it, pass a reference to the current instance of Form1 into the constructor. You can then access the methods on the existing instance of Form1. Here is an example:

    Form 1

    private void ChangeName_Click(object sender, EventArgs e)
    {
        // Pass a reference to this form instance using the 'this' keyword
        Form2 _Form2 = new Form2(this);
        _Form2.Show();
    }
    

    Form 2

    namespace Test
    {
        public partial class Form2 : Form
        {
            // Hold an instance to Form1
            private Form1 mForm1;
    
            public Form2(Form1 form1)
            {
                // Store instance to form1
                mForm1 = form1;
    
                InitializeComponent();
            }
    
            private void NewName_Click(object sender, EventArgs e)
            {
                // Change name on existing instance of form 1
                mForm1._Server("TEST");
                this.Close();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to the C programming language but I know that it is
Im new to programming and I dont know very much about but I'm making
i am new to servlet programming. i want to know that... is it possible
I'm kinda new at both programming, and WPF. I know that this is pretty
I know that it is poor programming and architecture when you have a class
I'm new to android programming, but one thing that I can't seem to get
I am new to programming. I know that a computer executes instructions in the
I'm still relatively new to Perl Programming, but I know how Perl 5 OO
I am not that really new in Programming but in Python I am really
Hii , I am relatively new to programming C++ . I do know that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.