5

I'm not experienced with advanced features of .NET type system. I cannot find out of what type is zz (what can be written instead of var. Or is var the best choice here?)

string foo = "Bar";
int cool = 2;
var zz = new { foo, cool };    // Watches show this is Anonymous Type

but most importantly, how the equivalent can be achieved in VB.NET code (this is what I actually need).

Dim Foo As String = "Bar"
Dim Cool As Integer = 2
Dim zz = {Foo, Cool}    'This is not an equivalent of above, I'm getting an array

I have searched several C# sources, used code watches and learned about how zz looks internally but I'm unable to make next step.

(The purpose of my effort is something like this in VB.NET.)

1
  • NOTE: This question is about object initializer of an anonymous type. If instead, you are looking for how to initialize properties, for an existing class, then you want to make object initializer of a named type. E.g. New Customer With {.Name = "Terry Adams", .City = "Louisville"} Commented May 23, 2017 at 15:13

1 Answer 1

8

Your code would not even compile with OPTION STRICT set to ON which is highly recommended. No type can be derived from String + Int32. It would compile if you want an Object():

Dim zz As Object() = {Foo, Cool}

But you want to create an anonymous type, use New With:

Dim zz = New With {Foo, Cool}

http://msdn.microsoft.com/en-us/library/bb385125.aspx


With .NET 4.7 and Visual Basic 2017 you can also use ValueTuples with names:

Dim zz = (FooName:=foo, CoolName:=cool)    

Now you can access the tuple items by name:

int cool = zz.CoolName;

https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/tuples

Sign up to request clarification or add additional context in comments.

7 Comments

This actually works, I overlooked simple With {...} I was already using elsewhere. From what you write I understand that the type cannot be narrowed any further. My attempt was to make string formatter refferring directly to variable names in its format string – and now it works great. But sacrificing my favourite Option Strict On – I'll consider twice whether I really want to continue with the above approach.
If you set OPTION INFER ON you can use anonymous types and don´t have to sacrifice OPTION STRICT ON
@NePh: but OPTION INFER ON (which i also recommend) doesn't prevent you from a compiler error if you try: Dim zz = {Foo, Cool}. Both types are "incompatible". So the error forces you to think twice what you want.
@miroxlav: i'm not sure if i'd understood your requirement. Can't you use String.Format which allows object or can't you use ToString to convert all to strings?
C# anonymous type properties are all readonly, so in VB you would need to specify 'Key' for all properties: Dim zz = New With {Key Foo, Key Cool} to achieve identical behavior as the original C# code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.