
dim and creating obj variables the right way?
"No question is dumb; not asking is what's dumb." --P. Navasoploulos.
All three of the following syntaxes do the same thing in VB.NET (instantiate
an object-bring it into existence):
Dim DataCon as New ADODB.Connection
Dim DataCon as ADODB.Connection = New ADODB.Connection
Dim DataCon as ADODB.Connection
DataCon = New ADODB.Connection
However, the third version gives you a bit more flexibility because the
object is not created in the declare (Dim) statement. It's declared in the
line where you use the New command. This way, you can declare the object
variable with Dim, but delay the actual instantiation of the object (with
New) until you actually need that object. If this distinction means
something to your programming, go ahead and use the most verbose format,
version three above. There is no significant difference if the line with NEW
immediately follows the line with DIM. But sometimes you might want to delay
instantiation until elsewhere in your code. One example is when you are
declaring a global variable, but not instantiating it until some procedure
is executed.
Quote:
> Hi
> This might seem like a really dumb question but here goes
> Is there a difference in the way the following object variables work when
> dimmed differently or is it a style thing
> I am in the habit of doing things like this
> Dim colCode As DataColumn = New DataColumn("ItemCode",
> Type.GetType("System.String"))
> but i see others doing this
> Dim colCode As DataColumn
> colCode = New DataColumn("ItemCode", Type.GetType("System.String"))
> Does this make a difference?
> I ask as VB6 say there was a subtle difference in the way objects where
> created depending on how they where dimmed and initialised
> Please dont patronize me too much :-)