Home > Enterprise >  .NET Object Initializers with numbers
.NET Object Initializers with numbers

Time:05-05

So I have the following for an object initializers. This is for an API I'm trying to send data too.

             Dim request = New With {
             .data = New With {
                .workspace = workspace,
                .name = "Complex task test",
                .notes = "These are task notes",
                .projects = {"1202219487026592"},
                .custom_fields = New With {
                    .21234515112 = "222"
                    }
                }
            }

The problem is the custom_fields requires the GIDs, which are numbers. Obviously we cannot use numbers for this so it causes issues.

Is there any way around this? Another way to declare this and have the same result?

CodePudding user response:

Using a Dictionary(Of UInt64, String) would work, assuming you're wanting the custom_fields property value to be serialized to a JSON object:

Dim request = New With {
    .data = New With {
        .workspace = workspace,
        .name = "Complex task test",
        .notes = "These are task notes",
        .projects = {"1202219487026592"},
        .custom_fields = New Dictionary(Of UInt64, String) From {
            { 21234515112, "222" }
        }
    }
}

Dim serializedJson = JsonConvert.SerializeObject(request)

Fiddle: https://dotnetfiddle.net/NHrNqC

  • Related