473,545 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1313
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(d t)

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.Co py

I hope this helps,

Cor

"Ronald S. Cook" <rc***@westinis .comschreef in bericht
news:Oh******** ******@TK2MSFTN GP04.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 =
SomeOtherExisti ngWhatever.

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******** ******@TK2MSFTN GP04.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
1418
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 why it has suddenly changed. The code hasn't altered as far as I'm aware but it is a possibility. The database is SQL based.
30
3523
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 no other choice but using "new")
3
1881
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
4134
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 c?
1
1338
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 GenericPrincipal object. So User.IsInRole("somerole") can be used. User/Roles Array first retrieved from DB through a stored procedure then cached....
1
1577
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 hangs and then eventually the application stops responding. I'm running on Windows XP Professional, I've made sure IIS is activated. Any Ideas?
4
2365
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 called. Alex
2
2817
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 then field names in the older versions. Has anyone figured out how to do this, ?
1
5334
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 needs two different sessions. When user clicks on a link in the existing session, application pops up new window and the new session should start...
0
7406
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7660
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7761
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5337
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1888
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.