0

Probably something stupid, but I'm trying to write values to an array of structures and keep getting "Object Reference not set to an instance of an Object". How do I initialize the array? The debug kind of code looks like this, all in the same form, VB.Net. When I Call WriteStepToStruct() I get the error.

Public Structure strutAnswers
    Public EvalID As Integer
    Public StepID As Integer
    Public Topic As Integer
    Public SubTopic As Integer
    Public Comment As String

End Structure

Public Shared CurrentRunData As strutAnswers()

Public Function WriteStepToStruct() as integer
    CurrentRunData(0).StepID = 1
    CurrentRunData(0).EvalID = 1
    CurrentRunData(0).Topic = 1
    CurrentRunData(0).SubTopic = 1
    CurrentRunData(0).Comment = "test"
Return 1
End Function

Regards, John

  • You need to give the array a size. A Class with properties would probably be better than a structure with fields; and a List would not require you know the size required like an array does – Ňɏssa Pøngjǣrdenlarp Nov 25 '16 at 14:27
  • As already suggest a `Class` would be better but this should solve your issue in the case; `Public Shared CurrentRunData(0) As strutAnswers` where `(0)` is the size of the array. – Bugs Nov 25 '16 at 14:30
  • 1
    Some general help on arrays: [When do I need to use a `New` keyword when creating an array](http://stackoverflow.com/a/35004563/1070452) – Ňɏssa Pøngjǣrdenlarp Nov 25 '16 at 14:35
  • 2
    @Jinx88909 : ...is the size of the array minus one. `(0)` will create an array with one element, `(32)` will create an array of 33 elements and so on. – Visual Vincent Nov 25 '16 at 14:35
  • See also: [Choosing Between Class and Struct](https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx) – Ňɏssa Pøngjǣrdenlarp Nov 25 '16 at 14:38
  • @VisualVincent, no if you did `(-1)` it would cause the error _Index was outside the bounds of the array._. However yes if you did `(32)` you would get 33 elements. Starting at `(0)` gives you one element, starting at `(1)` would give you two elements etc. Is that what you mean? Personally I tend to stay away from arrays and use `List(Of T)`. – Bugs Nov 25 '16 at 14:40
  • 1
    @Jinx88909 You contradicted yourself. "where (0) is the size of the array". "Starting at (0) gives you one element". Or the first statement wasn't quite what you meant and it got misinterpreted – A Friend Nov 25 '16 at 14:47
  • 2
    @ProGrammer, sorry yes my comment is misleading. _Public Shared CurrentRunData(0) As strutAnswers which will initialise 'structAnswers' with one element._ (Thanks VisualVincent, glad someone is reading my comments :)) – Bugs Nov 25 '16 at 14:51
  • @Jinx88909 : I didn't say you should do `(-1)`, I said that `(0)` is the size of the array minus 1 :). The size of the array is 1, `1-1 = 0`. The best way to know for sure, if one is uncertain about what to write, is to append `- 1` to the size declaration. For example: `(32 - 1)` gives you an array with 32 items. – Visual Vincent Nov 25 '16 at 14:56
  • 1
    @VisualVincent I misinterpreted what you had said, sorry. ProGrammer pointed out the stupidity of my comments which put your comment into context for me. – Bugs Nov 25 '16 at 14:58
  • 1
    @Jinx88909 : They weren't stupid, just a bit misleading. (; – Visual Vincent Nov 25 '16 at 15:01

0 Answers0