开发者

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 th开发者_运维技巧e 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;
}


Not only you can't do that, but in your code after the execution of setBtn_Click_1 the object of type Class1 that you created is gone - this is because you only have a reference to it in the method, so when the method executes the reference is gone!


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜