Results 1 to 1 of 1

Thread: [VB2008+] Initialiser Syntax

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    [VB2008+] Initialiser Syntax

    C# version here.

    VB2008 introduced initialiser syntax, which allows you to initialise the property values of a newly created object in a With block that is basically attached to the constructor, e.g.
    VB.NET Code:
    1. Dim myObject As New SomeType With {.Property1 = value1, .Property2 = value2}
    You might ask what's the use of this. Why is it any better than doing this:
    VB.NET Code:
    1. Dim myObject As New SomeType
    2.  
    3. With myObject
    4.     .Property1 = value1
    5.     .Property2 = value2
    6. End With
    Well, it's a bit more succinct than that for a start, but that's not where the real advantage lies. The property (or field) initialisation is actually part of the expression that returns a reference to the newly created object. As such you can use initialiser syntax in places where you couldn't use a With block or a more conventional pattern because it would require multiple lines of code. One example is when initialising member variables, e.g.
    VB.NET Code:
    1. Private myObject As New SomeType With {.Property1 = value1, .Property2 = value2}
    Normally you'd have to set the properties in the form's Load event handler or something like that.

    Another place where this is useful is when you want to create one object and then pass it to the constructor, or some method, of a second, but you want to set one or more properties of the first object. Previously you'd have had to do something like this:
    VB.NET Code:
    1. Dim obj1 As New SomeType
    2.  
    3. obj1.SomeProperty = someValue
    4.  
    5. Dim obj2 As New SomeOtherType(obj1)
    Now you can do this:
    VB.NET Code:
    1. Dim obj2 As New SomeOtherType(New SomeType With {.SomeProperty = someValue})
    This is not a revolutionary change, but it does let you keep your code more concise in certain circumstances.

    One thing I would caution against is using initialiser syntax in situations where it actually makes your code harder to read, which may happen if you nest too deeply or have lines that are too long. To help avoid this in certain circumstances, I strongly recommend using line continuation to make your code more readable, e.g.
    VB.NET Code:
    1. Dim myObject As New SomeType With {.Property1 = value1, _
    2.                                    .Property2 = value2}
    Last edited by jmcilhinney; Nov 26th, 2008 at 02:38 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width