My Software Notes

Useful things I discover

VB.NET Syntax for Object Initializers

leave a comment »

I don’t use VB.NET very much, so I keep forgetting the object initializer syntax.  So for my own benefit (and anyone else’s who has the same problem) here it is:

Dim acct = New Account() With { .Id = 10, .Name = "Joe Schmoe" }

Array initializer syntax is more like the C# syntax:

Dim intArray As Integer = { 1, 2, 3 }

And combining the two:

Dim acctArray() As Account = { _
                             New Account() With { .Id = 1, Name = "Mary Contrary" }, _
                             New Account() With { .Id = 2, Name="Fred Flintstone" } _
                       }

I won’t make any comments about how ugly VB.NET is because that would just start an argument and I’m a peace loving guy 🙂

References:

MSDN: VB 9.0 Object and Array Initializers
Wriju’s Blog: VB.NET 9.0: Object and Array Initializers

.

Advertisements

Written by gsdwriter

July 31, 2010 at 3:36 pm

Posted in Languages

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: