M
Mark Berry
Hi,
I'm moving from VB6 to VB 2005. Two questions:
1. One book I'm using has syntax like
Dim testConnection as SqlConnection = New
SqlConnection(connectionString)
However, the Help for the Dim statement says, "If you use New, you do not
use an initializer expression. Instead, you supply arguments, if required,
to the constructor of the class from which you are creating the variable."
And in fact, it seems that the following code is also allowed:
Dim testConnection as New SqlConnection(connectionString)
Are these actually identical? Is the latter example the preferred approach?
2. If the constructor has no arguments, I generally see code like this:
Dim testCommand As SqlCommand = testConnection.CreateCommand()
However when I type this into the IDE up to the "CreateCommand" and press
Enter, the IDE leaves off the parentheses:
Dim testCommand As SqlCommand = testConnection.CreateCommand
This seems very strange, since when I call a method without assigning its
result, the IDE adds the parentheses, e.g. I can type up to Open and press
Enter, and the IDE will add the parentheses:
testConnection.Open()
So again I am wondering if the Dim statements with and without the
parentheses is the same and whether one is preferred over the other.
Thanks for any clarifications.
Mark
I'm moving from VB6 to VB 2005. Two questions:
1. One book I'm using has syntax like
Dim testConnection as SqlConnection = New
SqlConnection(connectionString)
However, the Help for the Dim statement says, "If you use New, you do not
use an initializer expression. Instead, you supply arguments, if required,
to the constructor of the class from which you are creating the variable."
And in fact, it seems that the following code is also allowed:
Dim testConnection as New SqlConnection(connectionString)
Are these actually identical? Is the latter example the preferred approach?
2. If the constructor has no arguments, I generally see code like this:
Dim testCommand As SqlCommand = testConnection.CreateCommand()
However when I type this into the IDE up to the "CreateCommand" and press
Enter, the IDE leaves off the parentheses:
Dim testCommand As SqlCommand = testConnection.CreateCommand
This seems very strange, since when I call a method without assigning its
result, the IDE adds the parentheses, e.g. I can type up to Open and press
Enter, and the IDE will add the parentheses:
testConnection.Open()
So again I am wondering if the Dim statements with and without the
parentheses is the same and whether one is preferred over the other.
Thanks for any clarifications.
Mark