[英]How to assign a value to only one property of a class-array?
我有一个类和一个数组。 该数组声明为类型:Class。
//Class
public class TestClass
{
public int testint { get; set; }
public string teststr { get; set; }
public TestClass( int _testint, string _teststr )
{
testint = _testint;
teststr = _teststr;
}
}
//Array declaration
TestClass[] MyArray = new TestClass[ 3 ];
现在我想做的(这仅用于测试-在使用我想工作的方法时,我想到的代码要大得多):
我想要设置数组单元的仅teststr或仅testint,如下所示:
MyArray[ 0 ].testint = 3;
这不会返回任何错误,但是如果我尝试打印此错误; 结果为空白(空-空)。
我正在将Blend 4与Silverlight结合使用-如果您知道如何分配单个属性(如我想要的那样),请提供帮助!
您必须创建TestClass的实例,因为您刚刚创建了一个空数组,即该数组有3个对空引用。
TestClass[] MyArray = new TestClass[ 3 ];
MyArray为{null, null, null}
,因此MyArray[0] == null
MyArray[0] = new TestClass(42, "42");
MyArray为{anObject, null, null}
MyArray[0].testint = 3; // this is valid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.