0

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)
1
  • For the record, those aren't "declarations," they're definitions. Your declaration is 'Dim sqlCommand As MySqlDataAdapter' Commented Aug 20, 2009 at 21:55

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Also, sometimes you want to declare a variable without creating an instance of it right away.
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.
@mudface, you can do the same this with descendant classes, IE, Dim myAnimal As Animal = New Dog("Spot")
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.