0

I have two structures:

Dim testTransakcje(1) As Transakcje

Private Structure Transakcje
        Public kontrahentNazwa As String
        Public listaTowarow() As Towary
End Structure

Public Structure Towary
        Public towarSymbol As String
        Public towarNazwa As String
        Public towarIlosc As Integer
        Public towarCena As Double
End Structure

and I want to assign some values like this:

testTransakcje(1) = New Transakcje

testTransakcje(0).listaTowarow(0).towarSymbol = "FFF"
testTransakcje(0).listaTowarow(0).towarNazwa = "Test"
testTransakcje(0).listaTowarow(0).towarIlosc = 4
testTransakcje(0).listaTowarow(0).towarCena = 44.55
testTransakcje(0).listaTowarow(1).towarSymbol = "GGG"
testTransakcje(0).listaTowarow(1).towarNazwa = "Test2"
testTransakcje(0).listaTowarow(1).towarIlosc = 5
testTransakcje(0).listaTowarow(1).towarCena = 96.55

I don't want to create object of Towary structure, I just want to do the assign in one line.

I have an error: "Object reference not set to an instance of an object"

I know that listaTowarow() is Nothing, but I don't know how initializate it.

1 Answers1

2

You can't do this. You can either initialize it from outside of the structure, or you can declare it as shared, which I'm not sure will help you:

Private Structure Transakcje
    Public kontrahentNazwa As String
    ' Declaring shared initialized field of type Towary
    Public Shared listaTowarow(1) As Towary   
End Structure
Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294
ilans
  • 2,177
  • 1
  • 23
  • 26
  • I don't know how many elements of listaTowarow() have to be declared, because I get data from database and I display them in GridView. – sam-wiesz-kto Sep 09 '14 at 06:37
  • OK :) I'l remember this for the next time.. tnx. – ilans Sep 09 '14 at 06:38
  • 1
    @paulinyan: Then use a list instead of an array. – Patrick Hofman Sep 09 '14 at 06:40
  • `testTransakcje(0).listaTowarow(0).towarSymbol = "FFF"` I don't see `towarSymbol` when listaTowarow is a List - `Public listaTowarow() As List(Of Towary)` How can I recall to that field? – sam-wiesz-kto Sep 09 '14 at 06:47
  • 1
    But even if it's a list, it has to be initialized from outside the structure. You cannon initialize non Shared members from within the structure definition. – ilans Sep 09 '14 at 07:19