开发者

How to Get the value of a datatype from Class of Project2 to Class of Project1? - Visual Studio 2010 C#

I have 1 solution consisting of 2 projects. 1 is Windows Application and the other is Class Library. In the class library, there's a class that the value of its datatype is needed by the Windows Application. I came up with this,

//Proj2
public class class1
{
    bool data1;  //supossed to be true but false in default
    public void method()
    {
        if (condition)
            data1 = true;
    }
 }

//Proj1
public class class2
{
    bool data2; //must be equal to data1

    public void method()
    {
        Proj2.Class1 class1 = new Proj2.Cla开发者_如何学运维ss1();
        data2 = class1.data1
        if (data2 == true)
            MessageBox.Show(data2.ToString());
    }
}

The Problem is, The output is always showing False instead of true. I'm sure 100% that the condition returns TRUE because I tested it. I think the problem is the new instance, the datatype is reset to default which is false. So how do i get the original value. Note: There are two projects. Thank you!


It looks like you're missing the call to proj1.class1.method that would set data1 to true

So it should be

public void method()
{
    Proj2.Class1 class1 = new Proj2.Class1();
    class1.method();
    data2 = class1.data1;
    if (data2 == true)
        MessageBox.Show(data2.ToString());
}

I think the problem is the new instance

Now perhaps you don't want to call class1.method() every time Class2.method is called. If this is true you'll probably want to look at implementing a singleton. Then it would just be

public void method()
{
    data2 = Proj2.Class1.Instance.data1;
    if (data2 == true)
        MessageBox.Show(data2.ToString());
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜