我有一個類和一個數組。該數組被聲明爲類型: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 - 請幫助,如果你知道如何分配單一的屬性,像我想在這裏!
我很驚訝,你沒有一個空指針異常...... 嘗試做' MyArray [0] = new TestClass(){testint = 3}'代替。 – Thomas 2013-04-30 12:57:25
我希望您想到的更大的代碼具有大寫的屬性名稱。 – 2013-04-30 12:59:57
@Thomas:我想這是因爲這段代碼參與了一些綁定機制(請參閱Blend 4 with Silverlight),所以NullPointerException應該由UI處理。 – 2013-04-30 13:02:54