Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Dim x as new / Set x = nothing


Reply
 
Thread Tools Display Modes
  #1  
Old 12-13-2004, 03:56 PM
Bolek's Avatar
Bolek Bolek is offline
Contributor
 
Join Date: Feb 2003
Location: Prague, Czech Republic
Posts: 596
Default Dim x as new / Set x = nothing


Hello,

can somebody explain to me the
Code:
dim x as new y
set x = nothing
stuff, please? I am pretty skilled at C++. Is it similar to the new/delete operators? Or does this increase some reference counters and the memory is freed by garbage collector sometimes later as in Java?

How is it with the lifecycle of objects declared with dim x as new? If I declare a local variable this way, will it be freed when I leave the procedure or will it stay allocated until I explicitely delete it? And what with variables global to a form? Will they be freed when I unload the form? Adn if a class object is a member of another class, will it be freed if the parent object is freed automatically?

Thanks for your response. I was unable to find good explanation of this. In VB manuals they simply use this syntax without explaining these things. That's why I like C++ better - you have to know more but you better understand what your program is doing.
__________________
Really good programmers write code this way:
COPY CON PROGRAM.ARJ
Reply With Quote
  #2  
Old 12-13-2004, 04:53 PM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,742
Default

Here's the answer as I understand it.
Code:
Dim x As New y
y is a class. It starts a new instance of the class. If you have used C++/Java, you're probably familiar with these. The lifecycle basically is: it exists until you set it equal to nothing. That's where
Code:
Set x = Nothing
Comes into play - this removes it from memory, ending it's "life". Does this answer your question?
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote
  #3  
Old 12-14-2004, 02:10 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,885
Default

One problem with the used declaration, the object will always exist even if you set it to Nothing.
Preferred syntax:
Code:
Dim x As y Set x = New y ' Set x = Nothing
Reply With Quote
  #4  
Old 12-14-2004, 07:29 AM
Diurnal Diurnal is offline
Enthusiast

Retired Leader
* Expert *
 
Join Date: Apr 2002
Location: Bellevue, WA.
Posts: 3,233
Default

Quote:
dim x as new y
is know as auto instancing. As soon as this line of code is run, VB will create a new variable x with the properties and attributes of y. Auto-instancing variables can't be tested against the Nothing value. In fact, as soon as you use one and try to set it to nothing, VB relentlessly creates a new instance of the object. For this reason alone, it is generally not a good practice to instanciate an object this way. That is why Flyguy's approach is the best method as it allows you to remove the object from memory at will. If an object is created with the As New key word and is referenced in the code, it really will exist until the application ends.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->