473,320 Members | 1,920 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,320 software developers and data experts.

When "As New" or not when instantiating?

So most of the time I need to write the following to instantiate an object:

Dim cnn As New SqlConnection()

But sometimes Intellisense tells me not to include the "new":

Dim dst As DataSet

When do I use "As New" and when don't I? Intellisense is good at letting me
know which is correct, but I'd like to understand better.

Thanks,
Ron
Oct 11 '06 #1
4 1303
If you might not need the object right away, or possibly might not need
it at all, or if you need a broader scope, then just

Dim dst As DataSet

Otherwise, if you need it right away and you will be throwing it out immediately when you're done with it, use New.

Tom
Ronald S. Cook wrote:
>So most of the time I need to write the following to instantiate an object:

Dim cnn As New SqlConnection()

But sometimes Intellisense tells me not to include the "new":

Dim dst As DataSet

When do I use "As New" and when don't I? Intellisense is good at letting me
know which is correct, but I'd like to understand better.

Thanks,
Ron

Oct 11 '06 #2
Ronald,

If you create a New dataset than you use the keyword New

dim ds as New Dataset
dim dt as New DataTable
ds.Tables.Add(dt)

dim ds as New Dataset
da.Fill(ds) 'it is filled not created.

However if you use the ds as a placeholder to put an object in, than you
create of course no New dataset.

dim ds as Dataset = MyOldDataset.Copy

I hope this helps,

Cor

"Ronald S. Cook" <rc***@westinis.comschreef in bericht
news:Oh**************@TK2MSFTNGP04.phx.gbl...
So most of the time I need to write the following to instantiate an
object:

Dim cnn As New SqlConnection()

But sometimes Intellisense tells me not to include the "new":

Dim dst As DataSet

When do I use "As New" and when don't I? Intellisense is good at letting
me know which is correct, but I'd like to understand better.

Thanks,
Ron


Oct 11 '06 #3
Dim Myvar as New Whatever - creates a variable Myvar, creates a new
Whatever, and sets myvar to reference Whatever.

Dim Myvar as Whatever - creates a variable myvar that can reference a
Whatever, but doesn't actually create a new Whatever, so Myvar
initially references Nothing. You can set Myvar to an instance of
Whatever later, for example Myvar = New Whatever, or Myvar =
SomeOtherExistingWhatever.

Some object types you cannot create a New instance of directly, they
can only be created through other objects. Indeed, Intellisense warns
you about that.

Oct 11 '06 #4

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:Oh**************@TK2MSFTNGP04.phx.gbl...
So most of the time I need to write the following to instantiate an
object:

Dim cnn As New SqlConnection()

But sometimes Intellisense tells me not to include the "new":

Dim dst As DataSet

When do I use "As New" and when don't I? Intellisense is good at letting
me know which is correct, but I'd like to understand better.
Dim x as Gizmo
this creates a variable named x that can reference an object of type Gizmo
at this point, however, x does not reference an object (its a null pointer)
x = new Gizmo()
this calls the constructor of the Gizmo class and now the variable x
references an object of type Gizmo.
the sub named new 'instantiates' an object of the class.
HTH
Oct 11 '06 #5

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

Similar topics

3
by: Jase | last post by:
For some reason all of a sudden "£" signs are displaying as "?" when the field is displayed. The display code is: <%=rs.Fields("WagesText")%> This was working fine before, so I have no idea...
30
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there...
3
by: Lawlee | last post by:
How can I trigger a e-mail when new data is entered in a table without a form? Thanks
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
1
by: WebMatrix | last post by:
Hello! I am working on a web application with Windows Authentication. In WindowsAuthentication_Authenticate event of Global.asax file a user is Authenticated and User/Roles Array is loaded into...
1
by: JQA Academia | last post by:
I've reinstalled twice now... I've got the Visual Studio.net Academic version 2003. During installation, I get no errors but when I try to open a new project from the inital start page, it just...
4
by: Alex Maghen | last post by:
Does a call to Response.Redirect result in an immediate exit from the current page function (i.e. a "return()")? I want to make sure that the statements after the Response.Redirect() do not get...
2
by: Accessed | last post by:
Hi All I cannot see for the life of me how to do a "Save As" when in table design ! Its not critical, but it used to be so handy when coding to be able to copy (then paste) the table name and...
1
by: =?Utf-8?B?YWxiZXJ0bHk=?= | last post by:
How can I create a new session from the existing session in asp.net when new IE session is opened using javascript's window.open method or user's hyperlink click? I have a web application which...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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)...
0
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....

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.