简体   繁体   中英

Get the value of a property from a Class in Form2, and that value have been set in Form1 in C#

Here is the scenario. I want to set the value of Server in Class1, i am setting the value in Form1. Then get the value of Server in Class1 in Form2. Here is what i have.

class Class1
{
    private string server;

    public string Server
    {
        get { return server; }
        set { server = value; }
    }
}

//Form1 where i want to set the value of server
private void setBtn_Click_1(object sender, EventArgs e)
{
    Class1 sample = new Class1();
    sample.Server = serverTxt.Text;
}

//Form2 where i want to get the value of server that i've set in Form1
private void setBtn_Click_1(object sender, EventArgs e)
{
    Class1 sample = new Class1();
    string serVer = sample.Server;
}

I know i can't have a value of server because i declared a new instance of Class1. But is there any way that i can still get the value of Server in Form2 that i have set in Form1?

Please spare with me, i am new in C#, thanks in advance guys :D

There are number of alternatives but static instance of Class1 would be easier.

In form1, declare/create static instance of Class1 class

//Form1 where i want to set the value of server
public static Class1 sample=new Class1();
private void setBtn_Click_1(object sender, EventArgs e)
{
    sample.Server = serverTxt.Text;
}

and in Form2,

//Form2 where i want to get the value of server that i've set in Form1
private void setBtn_Click_1(object sender, EventArgs e)
{
    string serVer = Form1.sample.Server;
}

不仅您不能这样做,而且在执行setBtn_Click_1之后的代码中,您创建的Class1类型的对象不见​​了-这是因为您在方法中仅具有对它的引用,因此当方法执行引用时离开了!

You could send it in a constructor when creating the second form. Something like this then

class Class1
{
    private string server;

    public string Server
    {
        get { return server; }
        set { server = value; }
    }
}

//form 1
private void setBtn_Click_1(object sender, EventArgs e)
{
    Class1 sample = new Class1();
    sample.Server = serverTxt.Text;
    prevForm = sample;
}



//form 2
private void setBtn_Click_1(object sender, EventArgs e)
{
    Class1 sample = new Class1{ Server=prevForm.Server };
}

For this you should keep the result or the reference to you first form somewhere so you can acces it later on

one solution to this is to declare the server property in the Calss1 as static

class Class1
{
    public static string Server { get; set; }
}

so that you can get its value between the two forms

private void setBtn_Click_1(object sender, EventArgs e)
{
    Class1.Server = serverTxt.Text;
}

private void setBtn_Click_1(object sender, EventArgs e)
{
    string serVer = Class1.Server;
}

use this only if you if you have one Server for all the instances of Class1

You have to set the value of serverTxt.Text in Form1 to the Global variable(the simpliest way). Then just take the value of this global variable in Form2

You can send the relevant data in the Form2 constructor and initialize it from Form1 (pass the data when you initialize Form2 in Form1)

[EDIT] You could also pass the information via a database that keeps that data or using an external file that both forms have access to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM