如何获取从 Project2 类到 Project1 类的数据类型的值? - Visual Studio 2010 C#

发布于 2024-12-05 18:43:20 字数 673 浏览 1 评论 0原文

我有 1 个由 2 个项目组成的解决方案。一个是Windows应用程序,另一个是类库。在类库中,有一个类,Windows 应用程序需要其数据类型的值。我想出了这个,

//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.Class1();
        data2 = class1.data1
        if (data2 == true)
            MessageBox.Show(data2.ToString());
    }
}

问题是,输出总是显示 False 而不是 true。我确信该条件 100% 返回 TRUE,因为我测试了它。我认为问题是新实例,数据类型被重置为默认值,这是错误的。那么我如何获得原始值。注:有两个项目。谢谢你!

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.Class1();
        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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛放△進行李 2024-12-12 18:43:20

看起来您缺少对 proj1.class1.method 的调用,该调用会将 data1 设置为 true

所以应该是

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

我认为问题出在新实例

现在也许您不想每次调用 Class2.method 时都调用 class1.method() 。如果这是真的,您可能需要考虑实现单例。那么就只是

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

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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文