如何获取从 Project2 类到 Project1 类的数据类型的值? - Visual Studio 2010 C#
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您缺少对
proj1.class1.method
的调用,该调用会将data1
设置为true
所以应该是
现在也许您不想每次调用 Class2.method 时都调用
class1.method()
。如果这是真的,您可能需要考虑实现单例。那么就只是It looks like you're missing the call to
proj1.class1.method
that would setdata1
totrue
So it should be
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