简体   繁体   English

从项目调用变量到另一个C#

[英]Calling a Variable from a project to another C#

i want to pass a value from a variable from a project to another after referencing project B to A, after referencing, i still don't know how to read it 在将项目B引用到A之后,我想将一个变量从一个项目的值传递到另一个项目,在引用之后,我仍然不知道如何读取它

    //Project A
namespace projectA{
public partial class hello : Form
{
public string value;
}
}
}

and for Project B 对于项目B

using projectA{
namespace projectB{
public string value2 = value;
}
}

please help :(, i already added project A to B through add reference and when i type projectA.... there is only one method shown which is the partial class 请帮助:(,我已经通过添加引用将项目A添加到B,并且当我键入projectA ....时,仅显示一种方法,该方法是部分类

It sounds to me like you want to declare a static class member , you can do it like this: 在我看来,您想要声明一个静态类成员 ,可以这样进行:

public partial class hello : Form
{
    public static string value;
}

And then you access it with hello.value , beware that this is bad design , by doing things like this you are going to make code that is hard to understand and hard to maintain. 然后使用hello.value访问它,请注意, 这是错误的设计 ,通过执行类似的操作,您将制作难以理解且难以维护的代码。

Of course maintainable code is not something you will accomplish if you are learning so that's not a big deal, you should first understand the basic concepts and learn what does it mean for something to be "static", what does it mean to "instantiate" a class, what are interfaces, abstract classes, events, delegates, lambda expressions, inheritance, extension methods, properties and so on 当然,如果您正在学习,那么可维护的代码并不是您将要完成的事情,所以这没什么大不了的,您应该首先了解基本概念,并了解对于“静态”而言这意味着什么,对“实例化”意味着什么。一个类,什么是接口,抽象类,事件,委托,lambda表达式,继承,扩展方法,属性等

--EDIT-- - 编辑 -

Just noticed how wrong the code in projectB is (not valid C#), see if you can understand the code below: 刚刚注意到projectB中的代码有多错误(无效的C#),请查看您是否可以理解以下代码:

using projectA;

namespace projectB{
    class YouMustHaveAClass { //C# does not allow you to declare variables that belong to no class
        public string value2 = hello.value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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