tags:

views:

158

answers:

2

I'm new to VB.NET programming. What I'm confused about is the different ways one can declare a variable. Would someone please explain the difference between the two declarations below?

Dim sqlcommand As MySqlDataAdapter = New MySqlDataAdapter(sql, db)

And:

Dim anotherSqlcommand As New MySqlDataAdapter(sql, db)
+3  A: 

There is no difference.

Sometimes you want to use the first method though if you want to take advantage of interfaces...

Dim myList As IList(Of Something) = New List(Of Something)

Instead of being restricted to List(Of Something)

Dim myList As New List(Of Something)
Cory Larson
Also, sometimes you want to declare a variable without creating an instance of it right away.
Joel Coehoorn
So using interfaces would be the only reason for using the first example? If so, if you are not using any interfaces, one should always use the second example?
Really, it doesn't change anything, but the second way is nicer, there is less duplication.
Meta-Knight
@mudface, you can do the same this with descendant classes, IE, Dim myAnimal As Animal = New Dog("Spot")
Nathan Koop
A: 

There's actually no difference between those two but if you need to get an object from another function you have to create it this way:

Dim sqlcommand As MySqlDataAdapter = CreateSqlDataAdapter(sql, db)

and you can't put new in there.

Peymankh