Uses of NEW key word

B

Bob Day

VS 2003, VB.net...

Sometimes I get confused about when it is appropriate to use the word NEW.
In the code snipped below "Dim DT_With_Changes As New DataTable" works fine
with the NEW or without the NEW . This is a shared procedure, so I put in
the word NEW thinking that as multiple threads accessed it, each would be
working with its on instance of DT_With_Changes and I would not have to
worry about issuels like Monitor.Enter/Exit(DataTable_To_Update).
Intuitievely, with the word NEW, if each time a thread hits the code below,
even if at the same time, each thread would be working with its own instance
of DT_With_Changes . However, this didn't work, and the threads were
stepping on each other so I added the
Monitor.Enter/Exit(DataTable_To_Update).

1) In many cases, the use of NEW is obvious. Below, to me any way, it is
not. Can someone give me and explaination of when NEW is appropriate, and
what it doesn't solve the multiple thread access problem below?

2) also, DT_With_Changes.Dispose fails at runtime, so I changed it to
DT_With_Changes = nothing. But I don't understand why it fails. You get an
error message of something like "trying to dispose of a non-instantiated
object". Does seting it to nothing do the about same thing as dispose?

Thanks!
bob Day
' create DataTable to hold only the changes in table

Dim DT_With_Changes As New DataTable

Try

System.Threading.Monitor.Enter(DataTable_To_Update)

' load with rows that have changes in table

DT_With_Changes = DataTable_To_Update.GetChanges()

If Not DT_With_Changes Is Nothing Then

' update rows

Rows_Updated = SQL_Data_Adapter.Update(DT_With_Changes)

End If


' all rows for this table set back to unmodified.

DataTable_To_Update.AcceptChanges()

Finally

System.Threading.Monitor.Exit(DataTable_To_Update)

End Try

' dispose of temporary data table

DT_With_Changes = Nothing
 
T

Tom Leylan

Bob Day said:
Sometimes I get confused about when it is appropriate to use the word NEW

Bob: It isn't really any different from any other variable type but it
happens behind the scenes so it looks confusing. Not that I can make it
much clearer but I'll try :)

dim i as integer
dim o as object

Both lines have set up a variable which can contain a value of the defined
type. All that means is that enough room has been set aside for the storage
of that type of data.

dim i as integer = 5
dim o as object = MyClass

This would both create enough room and in the case of the integer put the
value 5 into that spot. In the case of the object it would fail of course
because MyClass isn't a datatype it's a bunch of instructions on how to
create and manipulate a datatype.

dim o as object = New MyClass

This time you instructed the MyClass class to actually contruct one... and
that object is what is being assigned as the value of place in memory known
as "o". This isn't be entirely unlike (if MS decided to change the syntax)
writing:

dim i as integer = New Integer(5)

And because the object can be of any size (unlike the integer) it doesn't
directly store the object data at that spot in memory, that's why objects
are known as a reference type. It stores a reference that points to
wherever the O/S stored the object.

So at some point say you have written a function that returns a MyClass
object, like GetMyClassObject.

The object exists already, that function called "New" and it is returning a
value equal to the reference (the address) of the object. In that case you
don't have to create a new object it has been done already, you only need to
create a spot large enough to hold the reference. So you don't call New.

dim o2 as object
o2 = GetMyClassObject()

Does that help? Or at least are you not more confused :)
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top