views:

89

answers:

2

I am trying to pick up on VB.net and have been programming in c# for a while. I have grasped pretty much most of vb.net but running into some issues with this conversion for object initialization:

CustomerParameters customerParameters = new CustomerParameters
                                               {
                                                   FirstName = "C First Name",
                                                   LastName = "C Last Name"
                                               };

Any thoughts on how to do this in VB, or if it is even possible?

+1  A: 

I can't check it here, because the syntax requires VS2008 and I only have VS2005. But in VB.Net you need to use the With keyword to do initialization.

Dim c As New CustomerParameters() With { _
    .FirstName = "C First Name", _
    .LastName = "C Last Name" _
}

Yes, that's right. Curly braces in VB.

Joel Coehoorn
And you need the line continuation character? Awful
JoshBerke
Heh, didn't your answer have a link to a blog post a second ago? And didn't it have the code snippet cut directly out of the blog post? That's a clever tactic. You sneak the answer in as fast as possible to be at the top of the list and then edit it. :) Very nice; I like it.
D. Patrick
@Josh: Hopefully, it'll go away in VB10.
Mehrdad Afshari
@D. Patrick - I did use someone else's code initially, but then changed it to match the op because it was already so close. The link no longer made sense. Getting to the top of the list wasn't my goal. If that were the case, I would have just posted the 2nd sentence, because finding, using, and attributing code on the web is relatively slow.
Joel Coehoorn
I'm not criticizing. I'm just havin' fun on a Friday. I only recognized it 'cause I used the same blog post. I'm a C# guy myself.
D. Patrick
@Mehrdad: I would hope so. In VB6 the With was a statement block (And yes I know it wasn't for initilization but I would see them as simillar from a grammer perspective)
JoshBerke
+4  A: 
Dim cp As New CustomerParameters() With { _
     .FirstName = "C First Name", _
     .LastName = "C Last Name" _
}
D. Patrick
I think its more readable to initialize the variable after the declaration... Dim cp As CustomerParameters = New CustomerParameters With {...
Boo