473,794 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should I Use "Dim ___ As New ___"?

Hi All,

I'm a VB6er who's recently started using VB2005; I have a general question
about variable declaration. In VB, I've always tried to avoid using the
"As New" construct when declaring a variable preferring instead to
instantiate in the class' or form's initialize event or as needed. Using
"As New" created unnecessary overhead and resulted in a loss of control
over my object's life cycle.

Is this advisable practice in .Net?
TIA
~Tim
Mar 15 '06 #1
17 1582
There is a bit more flexibility in declaring first and instantiating second
that *may* be usefull in certain scenarios. Other than this, it doesn't
matter.

"Tim Baur" <tr**********@A RDyahoo.com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
Hi All,

I'm a VB6er who's recently started using VB2005; I have a general question
about variable declaration. In VB, I've always tried to avoid using the
"As New" construct when declaring a variable preferring instead to
instantiate in the class' or form's initialize event or as needed. Using
"As New" created unnecessary overhead and resulted in a loss of control
over my object's life cycle.

Is this advisable practice in .Net?
TIA
~Tim

Mar 15 '06 #2
jvb
Using New is required except when using shared members of the class.
The new keyword creates a reference to the object that you are using.

Mar 15 '06 #3
"Tim Baur" <tr**********@A RDyahoo.com> schrieb
Hi All,

I'm a VB6er who's recently started using VB2005; I have a general
question about variable declaration. In VB, I've always tried to
avoid using the "As New" construct when declaring a variable
preferring instead to instantiate in the class' or form's initialize
event or as needed. Using "As New" created unnecessary overhead and
resulted in a loss of control over my object's life cycle.

Is this advisable practice in .Net?


The behavior is different. In VB6, "As New" means that the
object is created as soon as the variable is accessed. In VB.Net this does
not exist anymore. Now it means that the object is created as soon as the
variable is created. So, if you write

dim bla as new form

in a procedure, it is exactly the same as writing

dim bla as form
bla = new form
If you declare it at class level, it is exactly the same as writing

dim bla as form
'...
sub New()
bla = new form
end sub
See also:
http://msdn.microsoft.com/library/en...ctCreation.asp
Though, it is sometimes not advisable, but this is personal taste. I always
assume that the order of declaration doesn't matter. This is not true
anymore if the declaration contains a New statement. Example:

dim con as new oledbconnection (...)
dim cmd as new oledbcommand(co n)

You see that the app will fail if you change the order because the 2nd
statement depends on the 1st one.

Armin

Mar 15 '06 #4
"Armin Zingler" <az*******@free net.de> wrote in
news:#m******** ******@TK2MSFTN GP10.phx.gbl:

The behavior is different. In VB6, "As New" means that the
object is created as soon as the variable is accessed. In VB.Net this
does not exist anymore. Now it means that the object is created as
soon as the ...

Thank you all for the input. Thanks for the thourough analysis, Armin.
This is good news -- I had hoped VB.Net would handle it this way.
Mar 15 '06 #5
Tim,

Dim ds as new Dataset
For each whatever
ds = new Dataset
'The first instanced object will only be cleaned up unused by the GC

Therefore you can do better
Dim ds as Dataset
For each whatever
ds = new dataset

Although when I don't need the dataset outside the loop I prefer even

For each whatever
dim ds as new Dataset

I hope this gives some ideas

Cor
"Tim Baur" <tr**********@A RDyahoo.com> schreef in bericht
news:Xn******** *************** ***********@207 .46.248.16...
Hi All,

I'm a VB6er who's recently started using VB2005; I have a general question
about variable declaration. In VB, I've always tried to avoid using the
"As New" construct when declaring a variable preferring instead to
instantiate in the class' or form's initialize event or as needed. Using
"As New" created unnecessary overhead and resulted in a loss of control
over my object's life cycle.

Is this advisable practice in .Net?
TIA
~Tim

Mar 15 '06 #6
"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in
news:uK******** ******@TK2MSFTN GP10.phx.gbl:

For each whatever
dim ds as new Dataset


In this example, ds scopes the loop and drops the reference on completion?
Nice, very C-ish.

This brings up a new question, if I followed the example above, wouldn't a
thousand whatevers result in ds being created a thousand times? A thousand
sets of variables on the stack, memory reservations, methods registered,
constructors running, etc.

If I instantiate ds outside the loop and reuse the reference, haven't I
saved effort?
Mar 15 '06 #7
"Tim Baur" <tr**********@A RDyahoo.com> schrieb:
I'm a VB6er who's recently started using VB2005; I have a general question
about variable declaration. In VB, I've always tried to avoid using the
"As New" construct when declaring a variable preferring instead to
instantiate in the class' or form's initialize event or as needed. Using
"As New" created unnecessary overhead and resulted in a loss of control
over my object's life cycle.


There is no difference between 'As New X()' and 'As X = New X()' in VB.NET
any more. Personally I prefer 'As New' because it makes code more compact.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 15 '06 #8
> "Tim Baur" <tr**********@A RDyahoo.com> schrieb
Hi All,

I'm a VB6er who's recently started using VB2005; I have a general
question about variable declaration. In VB, I've always tried to
avoid using the "As New" construct when declaring a variable
preferring instead to instantiate in the class' or form's initialize
event or as needed. Using "As New" created unnecessary overhead and
resulted in a loss of control over my object's life cycle.

Is this advisable practice in .Net?

The behavior is different. In VB6, "As New" means that the
object is created as soon as the variable is accessed. In VB.Net this
does
not exist anymore. Now it means that the object is created as soon as
the
variable is created. So, if you write
dim bla as new form


Not only that, but in VB6 if you re-referenced a variable which was previously
set to nothing, you would re-instantiate it. This was the main reason to
avoid the Dim x as New foo in VB 6. You no longer need to worry about that.

Jim Wooley
Mar 15 '06 #9
Tim,

This brings up a new question, if I followed the example above, wouldn't a
thousand whatevers result in ds being created a thousand times? A
thousand
sets of variables on the stack, memory reservations, methods registered,
constructors running, etc.

If I instantiate ds outside the loop and reuse the reference, haven't I
saved effort?


That was why I showed it, in all those except the first will the same amount
of ds be instanced. In the first sample is that plus 1. However why bother
you are using managed code that is especially for this.

Cor
Mar 15 '06 #10

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

Similar topics

4
21556
by: Monte | last post by:
It compiles fine in one of my ms2002 databases, but on another db (I inherited from somebody else), I get this compile error: "User-defined type not defined" Is something turned off in this second db or something? Thanks much
3
17002
by: Bart Lateur | last post by:
For some reason, "Database" isn't recognised by Access as a built-in/predefined type, in Access' own VBA. Whenever I try to compile a line like Dim db as Database I get the complaint that that user defined type ("database") isn't defined. This happens both in Access 97 (on Win98) and in Access 2000 (on XP). I seem to recall this used to work on Access 2. For the rest, Access works normally.
2
1896
by: Peter Ignarson | last post by:
Hi, simple question Is there a syntax (other than the one below that is invalid) that will let me declare and initialize two variables at the same time (using VS 2003, 1.1) ? Thank you Pete Dim a,b As Integer = 6 'Syntax error, invalid
4
5541
by: Vincent Finn | last post by:
Hi, in some code I have there is a variable declared as Dim as String what is the for in this case? I thought it was an array at first but as far as I can tell the variable is just a string
3
1022
by: Tony Bansten | last post by:
Occasionally I saw VBS script where local variables were declared on top with a statement like DIM myvar However most of my own vbs script run without such declaration. When do I need them and when not? Tony
11
4239
by: Anil Gupte/iCinema.com | last post by:
When I use this Dim instance As New Timer I get the error: Error 1 Overload resolution failed because no accessible 'New' accepts this number of arguments. Yet, in the help section for Timer (in VB 2005) this is exactly the syntax shown. I also tried:
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3720
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.