当另一个类称它时,属性丢失了 [英] Property lost when another class call it

查看:59
本文介绍了当另一个类称它时,属性丢失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三节课。第一类我声明属性,第二类我设置属性值,第三类使用它的值。但是在第三类属性是空的。

class1

 首先
{
public string first { get ; set ;}
}



class2

第二类
{
public second()
{
first f1 = new first();
f1.first =你好;
}
}
< / pre >



class third

 class third 
{
public third()
{
first f1 = new first();
string nn = f1.first;
}
}





string nn show null。我该怎么办?

解决方案

第二中,您首先创建一个的实例。我第三你创建了另一个的实例。这是两个不同的例子。该属性不是 static ,因此每个实例都为其分配了不同的内存空间。 Sou要么共享一个单独的实例,要么使用静态属性。

但我无法告诉你选择哪条路径,因为你的帖子相当理论。


如果您希望Forms能够访问另一个表单中Field / Property(变量)的值(内容),然后您有两个必要的选择:



1. make您希望共享的表单中的字段/属性'静态:然后该字段成为类的字段,而不是类的每个实例的字段。 pre lang =c#> public class FirstClass
{
public static string FirstString {获得; set ;}
}

public class SecondClass
{
string valueOfFirstStringInFirst = FirstClass.FirstString;
}

// 测试
FirstClass fc = new FirstClass();
FirstClass.FirstString = hello;
SecondClass sc = new SecondClass();
Console.WriteLine(sc.valueOfFirstStringInFirst);

请注意,您必须使用类名访问静态属性'FirstString,而不是类的实例名称('fc)。



2.一个类访问字段的值(内容)的其他方式/无论在另一个类中哪个不涉及使用静态包括:



a。通过使用两个类实现的接口

b。通过将实例的引用注入其他类的实例

c。通过公开间接访问值的公共方法(内容)


i have three class. First Class where i declare property,in second class i set prperty value and third class for use its value.but in third class property is empty.
class1

class first
{
public string first{get; set;}
}


class2

class second
{
public second()
{
  first f1=new first();
  f1.first="hello";
}
}
</pre>


class third

class third
{
public third()
{
  first f1=new first();
  string nn=f1.first;
}
}



string nn show null. what should i do?

解决方案

In second you create an instance of first. I third you create an other instance of first. These are two different instances. The property is not static, thus every instance has a different memory space allocated for it. Sou either share one single instance or use static property.
But I can't tell you which path to choose, as your post is rather theoretic.


If you want Forms to have access to the value (contents) of a Field/Property (variable) in another Form, then you have two essential choices:

1. make the Field/Property in the Form you wish to share 'static: then that Field becomes a Field of the Class, rather than a Field of each instance of the Class.

public class FirstClass
{
    public static string FirstString {get; set;}
}

public class SecondClass
{
    string valueOfFirstStringInFirst = FirstClass.FirstString;
}

// test it
FirstClass fc = new FirstClass();
FirstClass.FirstString = "hello";
SecondClass sc = new SecondClass();
Console.WriteLine(sc.valueOfFirstStringInFirst);

Notice that you had to access the static Property 'FirstString using the Class name, not the name of the instance ('fc) of the Class.

2. Other ways for one Class to access the values (contents) of Fields/Whatever in another Class which do not involve using static include:

a. by using Interfaces which both Classes implement
b. by injecting references to instances into instances of other Classes
c. by exposing public methods that indirectly access the value (contents)


这篇关于当另一个类称它时,属性丢失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆