I have som auto generated (from xsd file, xsd.exe is used) Partial classes.

On of the properties is an arry, her is the code:

Partial Public Class Betaleregrp4900
   Private oversiktOverBetaleregrp4899Field() As OversiktOverBetaleregrp4899
   Private betalingTotaldatadef21821Field As BetalingTotaldatadef21821
   Private betalerAntalldatadef21822Field As BetalerAntalldatadef21822
   Private gruppeidField As String
   Public Sub New()
       Me.gruppeidField = "4900"
   End Sub
4899")>  _
   Public Property OversiktOverBetaleregrp4899() As
           Return Me.oversiktOverBetaleregrp4899Field
       End Get
           Me.oversiktOverBetaleregrp4899Field = value
       End Set
   End Property

This property is an array:

Private oversiktOverBetaleregrp4899Field() As OversiktOverBetaleregrp4899

I can write:

Skjema_Obj.Betaleregrp4900.OversiktOverBetaleregrp4899 = New
OversiktOverBetaleregrp4899() { _
       New OversiktOverBetaleregrp4899, _
       New OversiktOverBetaleregrp4899}

But my problem is, I don't know how many elements of the type
OversiktOverBetaleregrp4899 I have to create and then I do not know how many
New OversiktOverBetaleregrp4899 I need between {}.

I need to use an loop that generats the number of New
OversiktOverBetaleregrp4899 I need, but how can I do that? Is there another
way to do this?

Her is the start of the code for the partial class

Partial Public Class OversiktOverBetaleregrp4899
   Private betalerFodselsnummerdatadef2305Field As
   Private betalerOrganisasjonsnummerdatadef21820Field As
   Private betalerNavndatadef2303Field As BetalerNavndatadef2303
   Private betalerAdressedatadef2304Field As BetalerAdressedatadef2304
   Private betalerPostnummerdatadef11795Field As
   Private betalerPoststeddatadef11796Field As BetalerPoststeddatadef11796
   Private betalingBelopdatadef2307Field As BetalingBelopdatadef2307
   Private gruppeidField As String
   Public Sub New()
       Me.gruppeidField = "4899"
   End Sub
datadef-2305")>  _
   Public Property BetalerFodselsnummerdatadef2305() As
           Return Me.betalerFodselsnummerdatadef2305Field
       End Get
           Me.betalerFodselsnummerdatadef2305Field = value
       End Set
   End Property

I hope somebody can help me with this! Thanks!

I don't understand what you're asking. Load the property array in to a List of generics, modify it, then call .ToArray() if you are having problems working with an array. Other than that I can't understand what you are asking.

I don't understand what you're asking. Load the property array in to a List of generics, modify it, then call .ToArray() if you are having problems working with an array. Other than that I can't understand what you are asking.

I will try to explain it some more:

It is someting like this I want to do:

Dim i As Integer = 0
        Dim NumberOfCustomers = 3
        Dim OversiktOverBer_Tabell(NumberOfCustomers - 1) As OversiktOverBetaleregrp4899

        While NumberOfCustomers > 0

            Skjema_Obj.Betaleregrp4900.OversiktOverBetaleregrp4899 {New Oversik}

            Dim OversiktOverBet_Obj As New OversiktOverBetaleregrp4899

            Dim BetalerOrganisasjonsnummerdatadef21820_Obj As New BetalerOrganisasjonsnummerdatadef21820
            BetalerOrganisasjonsnummerdatadef21820_Obj.Value = "Test BetOrgNr (0) - 123456789"

            Dim BetalerFodselsnummerdatadef2305_Obj As New BetalerFodselsnummerdatadef2305
            BetalerFodselsnummerdatadef2305_Obj.Value = "Test BetFørslenr (0) - 07097645669"

            Dim BetalerNavndatadef2303_Obj As New BetalerNavndatadef2303
            BetalerNavndatadef2303_Obj.Value = "Test BetNavn (0) - Kari Normann"

            Dim BetalerAdressedatadef2304_Obj As New BetalerAdressedatadef2304
            BetalerAdressedatadef2304_Obj.Value = "Test BetAdr (0) - Flatbygdi"

            Dim BetalerPostnummerdatadef11795_Obj As New BetalerPostnummerdatadef11795
            BetalerPostnummerdatadef11795_Obj.Value = "Test BetPostNr (0) - 6893"

            Dim BetalerPoststeddatadef11796_Obj As New BetalerPoststeddatadef11796
            BetalerPoststeddatadef11796_Obj.Value = "Test BetPostSted (0) - Vik i Sogn"

            Dim BetalingBelopdatadef2307_Obj As New BetalingBelopdatadef2307
            BetalingBelopdatadef2307_Obj.Value = "2330"

            OversiktOverBet_Obj.BetalerOrganisasjonsnummerdatadef21820 = BetalerOrganisasjonsnummerdatadef21820_Obj
            OversiktOverBet_Obj.BetalerFodselsnummerdatadef2305 = BetalerFodselsnummerdatadef2305_Obj
            OversiktOverBet_Obj.BetalerNavndatadef2303 = BetalerNavndatadef2303_Obj
            OversiktOverBet_Obj.BetalerAdressedatadef2304 = BetalerAdressedatadef2304_Obj
            OversiktOverBet_Obj.BetalerPostnummerdatadef11795 = BetalerPostnummerdatadef11795_Obj
            OversiktOverBet_Obj.BetalerPoststeddatadef11796 = BetalerPoststeddatadef11796_Obj
            OversiktOverBet_Obj.BetalingBelopdatadef2307 = BetalingBelopdatadef2307_Obj

--> This is may problem, I have to do this another way. Is it possible to split this sentence. The array/list Skjema_Obj.Betaleregrp4900.OversiktOverBetaleregrp4899 must have in this case 3 OversiktOverBetaleregrp4899 objekst, and I have to say {New OversiktOverBetaleregrp4899, New OversiktOverBetaleregrp4899, New OversiktOverBetaleregrp4899}
but how do I do that when the number of objects is not static?

            [B]Skjema_Obj.Betaleregrp4900.OversiktOverBetaleregrp4899 = New OversiktOverBetaleregrp4899() {New OversiktOverBetaleregrp4899}[/B]            Skjema_Obj.Betaleregrp4900.OversiktOverBetaleregrp4899(i) = OversiktOverBet_Obj

            OversiktOverBer_Tabell(i) = OversiktOverBet_Obj

            i = i + 1

            NumberOfCustomers = NumberOfCustomers - 1

        End While

I hope this made it clearer. Thanks!

Neither did I get this :)

But if you don't know the number of array elements at the compile time, pass it to the constructor of your (partial) class.

I would do something like this

Partial Public Class CItem1

  Public Prop1 As Integer
  Public Prop2 As String

  Public Sub New(ByVal value1 As Integer, ByVal value2 As String)
    Prop1 = value1
    Prop2 = value2
  End Sub

End Class

Partial Public Class CItem2

  Public Prop3 As Integer
  Public Prop4() As CItem1

  Public Sub New(ByVal value3 As Integer, ByVal NumOfElements As Integer)
    Prop3 = value3
    ReDim Prop4(NumOfElements)
  End Sub

End Class

and use it

Dim OneItem As CItem2

OneItem = New CItem2(3, 2) ' Now Prop3 has the value 3 and we have an uninitialized array with three CItem1s

' Set some values
OneItem.Prop4(0).Prop1 = 1
OneItem.Prop4(0).Prop2 = "Cust1"
' Or...
OneItem.Prop4(1) = New CItem1(2, "Cust2")

I did read your code and tried to grab the idea what you're trying to do. This may not be exactly what you wanted, but I hope it gives some idea.


This article has been dead for over six months. Start a new discussion instead.