Items can be the result of a constructor, a method call, a property access. It can also be mixed with Object initializer.
It is not possible to use Object initializer syntax AND collection initializer syntax for the same object at the same time. For example, these won't work
We can also allow collection initializer syntax by providing for a custom type.
It must implement IEnumerable and have an accessible and compatible by overload rules Add method (instance, Shared or even extension method)
Contrived example :
If we wanted to add Person object to a List(Of Person) by just putting the name in the collection initializer (but we can't modify the List(Of Person) class) we can use an Extension method
The first interesting thing to know is how to write comments.
In VB .NET, you write a comment by writing an apostrophe ' or writing REM. This means the rest of the line will not be taken into account by the compiler.
Declaring a Variable
In VB.NET, every variable must be declared before it is used (If Option Explicit is set to On). There are two ways of declaring variables:
Inside a Function or a Sub:
See this answer for full details about Option Explicit, Strict and Infer.
Inside a Class or a Module:
These variables (also called fields in this context) will be accessible for each instance of the Class they are declared in. They might be accessible from outside the declared Class depending on the modifier (Public, Private, Protected, Protected Friend or Friend)
These fields can also be declared with Dim but the meaning changes depending on the enclosing type:
One interesting thing is the ability to add you own comments into Visual Studio Intellisense. So you can make your own written functions and classes self-explanatory. To do so, you must type the comment symbol three times the line above your function.
Once done, Visual Studio will automatically add an XML documentation :
After that, if you type in your Test function somewhere in your code, this little help will show up :
Modifiers are a way to indicate how external objects can access an object's data.
Means any object can access this without restriction
Means only the declaring object can access and view this
Means only the declaring object and any object that inherits from it can access and view this.
Means only the delcaring object, any object that inherits from it and any object in the same namespace can access and view this.
Is equivalent to
Anonymous Types (Option Infer must be On)
Although similar anonymousInstance doesn't have same type as someInstance
Member name must be unique in the anonymous type, and can be taken from a variable or another object member name
Each member can be preceded by the Key keyword. Those members will be ReadOnly properties, those without will be read/write properties
Two anonymous instance defined with the same members (name, type, presence of Key and order) will have the same anonymous type.
Anonymous types are structurally equatable. Two instance of the same anonymous types having at least one Key property with the same Key values will be equal. You have to use Equals method to test it, using = won't compile and Is will compare the object reference.
Both Named and Anonymous types initializer can be nested and mixed
Writing a function
A function is a block of code that will be called several times during the execution. Instead of writing the same piece of code again and again, one can write this code inside a function and call that function whenever it is needed.
A function :
Must be declared in a class or a module
Returns a value (specified by the return type)
Has a modifier
Can take parameters to do its processing
A Function Name, could be used as the return statement
is just the same as
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0