dim and creating obj variables the right way? 
Author Message
 dim and creating obj variables the right way?

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 :-)



Tue, 27 Jan 2004 18:24:17 GMT  
 dim and creating obj variables the right way?
Of course this includes

Dim colCode As new DataColumn("ItemCode", Type.GetType("System.String"))

What i want to know is does Dotnet handle the differnet methodes differently
and are there any gotcha's etc or does it just boil down to typing?

Thanks

Mark


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 :-)



Tue, 27 Jan 2004 19:08:49 GMT  
 dim and creating obj variables the right way?
Mark,

NO difference, just preference.

--
Kathleen
(MS-MVP)
Reply in the newsgroup so everyone can benefit
--



Tue, 27 Jan 2004 23:29:54 GMT  
 dim and creating obj variables the right way?

Thanks

thats one more thing made clearer

Mark


Quote:
> Mark,

> NO difference, just preference.

> --
> Kathleen
> (MS-MVP)
> Reply in the newsgroup so everyone can benefit
> --



Tue, 27 Jan 2004 23:52:38 GMT  
 dim and creating obj variables the right way?
Mark,
Actually I believe there is a difference! ;-)

Quote:
>     Dim colCode As DataColumn = New DataColumn("ItemCode",
> Type.GetType("System.String"))
> Dim colCode As new DataColumn("ItemCode", Type.GetType("System.String"))

The above two should offer the same performance, I prefer the second one.

Quote:
>     Dim colCode As DataColumn
>     colCode  =  New DataColumn("ItemCode", Type.GetType("System.String"))

The above I would expect to be (slightly) slower, doesn't VB.Net initialize
colCode to
Nothing in the first statement, then set its value in the second. So you may
actually be running extra instructions here...

Just a thought
Jay


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 :-)



Wed, 28 Jan 2004 05:26:43 GMT  
 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 :-)



Wed, 28 Jan 2004 16:21:35 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Dim obj As New Class crt Dim Obj As Class = New Class

2. dynamically dim a variable ie dim "variable"&n

3. Input #1, obj.nNumber causes error: variable required

4. All the possible ways to create an attachment

5. Obj Ref not set to an instance of an Obj

6. 3 ways to create a Word object?

7. CR Engine Obj Lib vs CR Designer Component Obj Model

8. COM+ obj calling another COM+ obj

9. Dim'ing a list of variables

10. Problem dimming a variable as a database!

11. I can create obj ! please read me

12. Dim variable

 

 
Powered by phpBB® Forum Software