Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the Difference between (As) and (=) in object declaration in VB.NET

I can create a new object like this:

Dim sqlconn As New SqlClient.SqlConnection(cs)

or like this:

Dim sqlconn = New SqlClient.SqlConnection(cs)

What's the difference? Since both worked fine for me!

like image 220
Rami Alshareef Avatar asked Jun 02 '11 08:06

Rami Alshareef


People also ask

What is the difference between module and class in VB net?

The main difference between classes and modules is that classes can be instantiated as objects while standard modules cannot.

What is Public Sub New in VB net?

Sub New() is the constructor of your class, which is executed to initialize an instance of the class. You can't call it after the object has been created.

What does <> mean in VB?

<> in VB.NET means "not equal to". It can be used with the normal oprands as well as in comparision with the items when compared with the datas fetched with the data reader (from database). Follow this answer to receive notifications.


1 Answers

The first one is the short form of:

Dim sqlconn As SqlClient.SqlConnection = New SqlClient.SqlConnection(cs)

The second one depends on what version of VB you are using. In VB 7 and VB 8 it is the same as:

Dim sqlconn As Object = New SqlClient.SqlConnection(cs)

In VB 9 type inference was introduced, so the compiler will infer the type from the assignment and produce the same code as the first one.

The type inference requires that the option Option Infer is set to on. This is the default setting, but it might be off if you migrate a project from an older version.

like image 71
Guffa Avatar answered Oct 06 '22 03:10

Guffa