简体   繁体   中英

VB.Net Declare an array of classes, and set value

I have a little syntax declare problem in VB.Net.

Dim proxy As USImportoerServiceTypeClient = DMRUtils.CreateAndConfigureClient()

Dim request As New USDeclare_I()
request.DeclareCollection = New US_ITypeDeclare() {}
For Each KES In request.DeclareCollection
    KES.DeclareCollectionStructure.DeclareCollectionValidDate = DateTime.ParseExact(txtDeclareDate.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
Next

This above code do not work, becase the "US_ITypeDeclare() {}" is empty an only contains a new DeclareCollectionStructure

How do I declare a KoeretoejErklaeringStructure to this an set this date value til "DeclareCollectionStructure.DeclareCollectionValidDate" ?

Best regards from Denmark

Arrays have a fixed length, which is declared when the array is created. Therefore your array will always have a length of 0 .

Use a List(Of T) instead. Lists grow dynamically when you add items to them.

request.DeclareCollection = New List(Of US_ITypeDeclare)
Dim newItem = new US_ITypeDeclare()
newItem.DeclareCollectionStructure.DeclareCollectionValidDate =  _
    DateTime.ParseExact(txtDeclareDate.Text, "dd-MM-yyyy", _
                System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
request.DeclareCollection.Add(newItem)
' Now the list contains one item

Of cause you will have to adapt the definition of DeclareCollection to be a list instead of an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM