开发者

Help with Class arrays

开发者 https://www.devze.com 2023-02-09 15:44 出处:网络
When the following execu开发者_如何学运维tes, I get the error \'NullReference Exception\' - Object reference not set to an instance of an object.TestClass has Get and Set methods for the integer prope

When the following execu开发者_如何学运维tes, I get the error 'NullReference Exception' - Object reference not set to an instance of an object. TestClass has Get and Set methods for the integer property TestWord. How should the following be changed to let me set TestWord in six elements of TArr?

Dim TArr(5) As TestClass

For i As Integer = 0 To 5
   TArr(i).TestWord = i * 10
Next


You need to initialize TArr with some array. If you don't do that, it's just a reference to an object that doesn't exist (that's why you get a null reference exception).

Dim TArr(5) As TestClass <---- This doesn't mean you'll have an array of TestClass filled with instances of TestClass. You need to assign your 5 TestClass instances.

You can do that in the For loop:

For i As Integer = 0 To 5
   TArr(i) = new TestClass()
   TArr(i).TestWord = i * 10
Next
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号