Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize Object Properties in One Line of Code

Tags:

c#

vb.net

Question:

Hello All,

Sorry that this is kind of a noob question. I just don't know how to word this process, so I'm not sure what to Google for. I'll put some C# code below that should explain what I'm trying to do. I just don't know how to do it in VB. Additionally, for future ref, if you could tell me what this process is called, it would be helpful to know. Thanks in advance for your help.

// Here is a simple class
public class FullName
{
    public string First { get; set; }
    public char MiddleInintial { get; set; }
    public string Last { get; set; }

    public FullName() { }
}

/* code snipped */

// in code below i set a variable equal to a new FullName 
// and set the values in the same line of code
FullName fn = new FullName() { First = "John", MiddleInitial = 'J', Last = "Doe" };
Console.Write(fn.First);  // prints "John" to console

As I mentioned earlier, I am drawing blanks on what to search for so sorry if this question is a repeat. I too hate reruns :) So, please link me somewhere else if you find something.


Solution:

So thanks to the help of one of our members, I have found that the keyword is With.

Dim fn As New FullName() With { .First = "John", .MiddleInitial = "J"c, .Last = "Doe" }
Console.Write(fn.First)  ' prints "John" to console
like image 934
regex Avatar asked Nov 11 '09 17:11

regex


People also ask

How do you initialize an object?

Objects can be initialized using new Object() , Object. create() , or using the literal notation (initializer notation). An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ( {} ).

How do you assign initial values to object properties when an object is created?

To create an object of a named class by using an object initializer. Begin the declaration as if you planned to use a constructor. Type the keyword With , followed by an initialization list in braces. In the initialization list, include each property that you want to initialize and assign an initial value to it.

How do you initialize an object in C++?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

What is object initialization in Java?

Initializing an object means storing data into the object. Let's see a simple example where we are going to initialize the object through a reference variable. File: TestStudent2.java.


3 Answers

This is an Object Initializer.

The equivelent VB.NET code would be:

Dim fn = New FullName() With {.First = "John", .MiddleInitial = 'J', .Last = "Doe" }

The VB.NET reference is on MSDN.

like image 99
Reed Copsey Avatar answered Oct 10 '22 16:10

Reed Copsey


This feature is named Object Initializers. See here: http://www.danielmoth.com/Blog/2007/02/object-initializers-in-c-30-and-vb9.html

like image 37
Konamiman Avatar answered Oct 10 '22 16:10

Konamiman


They are known as object initializers. You can find more information on them here.

like image 26
Karmic Coder Avatar answered Oct 10 '22 16:10

Karmic Coder