Search code examples
.net.net-2.0generic-listgeneric-collections

.Net 2.0 List Issue


Getting an error, hopefully that can be fixed.

I have a Strongly typed list (or at least I am attempting to use one rather...) It only contains 2 properties... NodeName (string), NodeValue(string)

The error I am getting is when trying to add to said list:

_Results.Add(New Calculator01.ResultTyping() With {.NodeName = "Number_Departures_Per_Day", .NodeValue = DC_NDPD.ToString()})

Is producing the following error:

error BC32017: Comma, ')', or a valid expression continuation expected.

Yes, this is an inherited .Net 2.0 website, and no, I cannot upgrade it to a newer version. (I asked the boss already)

I am open to using a different generic collection though, so long as I can strongly type it...


Solution

  • Object Initializers were introduced with Visual Studio 2008 so they were simply not available in .NET 2.

    But you can use this syntax:

    Dim calculator As New Calculator01()
    calculator.NodeName = "Number_Departures_Per_Day"
    calculator.NodeValue = DC_NDPD.ToString()
    _Results.Add(calculator) 
    

    If you want one line you should provide an appropriate constructor which is a good thing in general:

    Class Calculator01
    
        Public Sub New(NodeName As String, NodeValue As String)
            Me.NodeName = NodeName
            Me.NodeValue = NodeValue
        End Sub
    
        Public Property NodeName As String
        Public Property NodeValue As String
    
    End Class
    

    Now you can use this code:

    _Results.Add(new Calculator01("Number_Departures_Per_Day", DC_NDPD.ToString()))