473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Uses of NEW key word

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
Nov 20 '05 #1
4 1042
1) on the new keyword
http://msdn.microsoft.com/library/de...newkeyword.asp
2) yes

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:Oa**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #2
EricJ wrote:
1) on the new keyword
http://msdn.microsoft.com/library/de...newkeyword.asp 2) yes ** Only if it the last reference to this object.

Regards - OHM

"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:Oa**************@TK2MSFTNGP09.phx.gbl...
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


Best Regards - OHMBest Regards - OHM On**********@BTInternet.Com
Nov 20 '05 #3
"Bob Day" <Bo****@TouchTalk.net> wrote...
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

Nov 20 '05 #4
Very helpful, thanks Tom!

Bob Day
------------------
"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:eV**************@tk2msftngp13.phx.gbl...
"Bob Day" <Bo****@TouchTalk.net> wrote...
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

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Thielen | last post by:
When we sat down to design a new generation of J2EE-based reporting tools, we naturally assumed that somewhere along the line we’d have to make compromises to satisfy both the technical staff and...
1
by: Luis Esteban Valencia | last post by:
I have a WebApplication (ASP.NET) that uses Word 2000 (COM , via Interop.Word.dll). I want to deploy it to a computer, but Word is not installed there. I tried to register the MSWORD.OLB (and...
2
by: William LaMartin | last post by:
I have created a program that allows for the automation of things in Word documents, like changing the values of DocVariables and the links to Excel Sheets. I did it using interoperoperatability,...
50
by: Shadow Lynx | last post by:
Consider this simple HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
37
by: cman | last post by:
Could you point me to the practical uses of XOR in assembly and algorithms? I understand XOR to be "true if and only if p is true or q is true". Where is this used? I draw a blank on usage. Tilak
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
5
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
11
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.