Inline Definition and Declaration
-
28-10-2019 - |
سؤال
Ok, im sure im mis-wording the concept but here it is anyways.
I know in CSharp you can do
el.AppendChild(new UISize(file, "TSize") { CX = 95, CY = 20 });
which declares a temporary bucket variable and then assigns the associate property values to the variable. Which then sends it to the XMLElement
AppendChild
method.
- What is this design concept called?
- What is the conversion to VB.Net?
I have tried using my own online utility that does a 90-95% conversion rate from C# <-> VB.Net. It has failed in this instance, and so need a hand-up on what i am looking to do to convert this from C# to VB.Net.
I would really like to not have to do a long-declaration of a variable with assignments, if at all possible.
المحلول
This is using object initializers in C#. The syntax to do the same is subtly different in VB
var uiSize = new UISize(file, "TSize") { CY = 95, CY = 20 }; // C#
Dim uiSize = New UISize(file, "TSize") With {.CX = 95, .CY = 20} ' VB
And, of course, you can inline the instantiation of the object inside a method call as well.
someObject.SomeMethod(New UISize(file, "TSize") With {.CX = 95, .CY = 20})
For more on using object initializers in Visual Basic, consult this documentation.