简体   繁体   English

从另一个类调用非静态变量

[英]Calling a non-static Variable from another Class

I want to call non-static variable from another class. 我想从另一个类调用非静态变量。 If I make it static, it affects my other code. 如果我将其设置为静态,则会影响其他代码。

I've two classes Harvest_Client and Harvest_Project. 我有两个类Harvest_Client和Harvest_Project。

In my Harvest_Project class I've 在我的Harvest_Project类中,

public int _client_id{ get; set;}

I'just want to do in Harvest_Client class is, 我只想在Harvest_Client类中做的是

public int _id = Harvest_Project._client_id;

How should I do this? 我应该怎么做?

Firstly, you should rename the property (it's not a variable) to conform with .NET naming conventions, eg 首先,您应该重命名属性(它不是变量)以符合.NET命名约定,例如

public int ClientId { get; set; }

Next, you need an instance of HarvestProject (post-renaming). 接下来,您需要一个HarvestProject 实例 (重新命名)。 Don't just create a new one - you need the right instance, the one whose client ID you're interested in. We can't tell you which one it is - but if you don't already have an instance of HarvestProject to hand, you should work out how you're expecting to specify which client ID you want. 不要只创建一个新实例-您需要正确的实例,一个您感兴趣的客户ID实例。我们无法告诉您它是哪个实例-但是,如果您还没有一个HarvestProject实例,一方面,您应该确定如何指定所需的客户端ID。

Think of it this way: if I were to ask you "How old is a person?" 这样想:如果我要问你“一个人多大?” you'd naturally want to know which person I was talking about. 你自然想知道我说的是哪个人。 It's exactly the same here. 这里完全一样。

maybe passing the reference to the class will do what you need: 也许将引用传递给类将满足您的需求:

public class Harvest_Project
    {
        public string Name { get; set; }
        public int clientId { get; set; }
    }

    public class Harvest_Client
    {
        private Harvest_Project MyInstance;

        private int myid;

        public int MyId
        {
            get
            {
                return myid;
            }
            private set
            {
                MyId = value;
            }
        }
        public Harvest_Client(Harvest_Project cls)
        {
            MyInstance = cls;
            MyId = cls.clientId;//since class reference present no 
                                //need for the property.
                                //its just here to show if in your
                                //project you really just need ID
                                //in this example its redundant
        }

    }

Depending on what you are trying to do you can make a List of Harvest_Project objects or better a dictionary,if there are various types of "projects" they could be all placed in a dictionary cataloged accordingly to the specified key. 根据您要执行的操作,可以创建Harvest_Project对象列表或更好的字典,如果存在各种类型的“项目”,则可以将它们全部放置在根据指定键进行分类的字典中。

One moment I would like to recommend to take into consideration the following declaration of property public int ClientId {get; private set;"} 我想建议您考虑一下以下属性声明public int ClientId {get; private set;"} public int ClientId {get; private set;"} It allow you to prohibited setting this variable from third party code, if you actually need this. Otherwise you will have ordinary global variable public int ClientId {get; private set;"} ,如果确实需要,它允许您禁止从第三方代码中设置此变量。否则,您将拥有普通的全局变量

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

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