Bernard Aybouts - Blog - MiltonMarketing.com

FAQ: What is the use of New Keyword in VB.NET?

FAQ

Approx read time: 1.9 min.

The New keyword in VB.NET is primarily used for creating instances of types—this includes both value types and reference types. The usage of New is crucial for initializing new objects, especially when dealing with classes and structures because it calls the appropriate constructor for the object, setting up the initial state.

Key Uses of the New Keyword

  1. Creating Instances of Classes: When you create an instance of a class, New allocates memory for that instance and invokes the constructor to initialize the object.
  2. Creating Instances of Structures: Even though structures are value types, you can use New to explicitly create instances of them, particularly to call a constructor that initializes the fields.
  3. Instantiating Arrays: New is used to create arrays by allocating memory for a specified number of elements.
  4. Object Initializers: From VB.NET 2010 onward, New can be used with object initializers to instantiate and initialize an object in a single expressive statement.

Code Snippet Examples

1. Creating an Instance of a Class

Consider a class Person with properties for FirstName and LastName. Here’s how you might use New to create an instance of this class:

Copy to Clipboard

2. Creating an Instance of a Structure

Here’s an example of using New with a structure:

Copy to Clipboard

3. Instantiating Arrays

Using New to create and initialize an array:

Copy to Clipboard

4. Object Initializers

Using New with object initializers to instantiate and initialize an object in one step:

Copy to Clipboard

Summary

The New keyword is fundamental in VB.NET for creating instances of types and for calling their constructors to properly initialize new objects. It ensures that objects are ready to use immediately after their creation, with all necessary initializations already handled. This behavior is vital for enforcing good programming practices around encapsulation and data integrity.

Leave A Comment


About the Author: Bernard Aybout (Virii8)

Avatar of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀