Kevin Evans Kevin Evans - 1 month ago 19
Vb.net Question

Comments between dictionary initialization lines in VB.NET

I have a multiline statement in VB.NET and I'd like to put comments in between the lines to help clarify some stuff.

Essentially, I'd like a comment to look something like this:

New PropertyThingy() From {
{"reallyLongDictionaryKeyNameThatIsExcessivelyLong", data.reallyLongPropertyName},
{"theApiMaintainerReallyLikesVerboseKeys", data.Blagh},
' I'd like to put a line here...
{"whyAreTheseReallyVerbose", data.SomeProperty},
}


Unfortunately, I can't get it to compile because of this error: "Syntax Error: Collection element initializer expected." Is there a way to get comments between the lines?

I've also tried this:

New PropertyThingy() From {
{"reallyLongDictionaryKeyNameThatIsExcessivelyLong", data.reallyLongPropertyName},
{"theApiMaintainerReallyLikesVerboseKeys", data.Blagh},
_ ' Some comment goes here:
{"whyAreTheseReallyVerbose", data.SomeProperty},
}

Answer

This seems the code that initialize a Dictionary. Then you don't need the underscore, but you still can't have the comment between the lines.

However you could write the comment on the same line

Dim c = New Dictionary(Of String, Integer)() From
{
    {"abc", 1}, ' Description of the first entry
    {"def", 2}  ' Description of the second entry
}