dim x as something

C

cj

I'm sure it's simple but it's new to me

I can dim x as new something which defines x as a new instance of
something in one statement. I can also dim x as something then later
make x = new something which does the same thing in a two step process.

Here's what I don't understand and really not sure how to ask but I want
to dim x as something when a program starts and this should be valid for
as long as the program is running. Then when I run a function I want it
to create an instance using x = new something. Finally when that sub is
finished how can I clean up or dispose of that instance I created when I
started the sub--keep in mind I still want the dim x as something I put
at the start of the program to be valid because I might want to rerun
that sub again.
 
M

Marina Levit [MVP]

Dim x As MyClass

This defines x as a variable that can point to an instance of MyClass.
However, at this point it does not point to anything at all.

Dim x As New MyClass

This defines x as a varaible that can point to a new instance of MyClass and
initializes it to point to a new instance of MyClass.

x= New MyClass

This points x to a new instance of MyClass. If it was pointing to an
instance already, then it no longer points to the old instance, but points
to the new one instead.

x = Nothing

This points x to nothing. This means x no longer points to an object (if it
pointed to one before), it is a null pointer.
 
T

Tom Leylan

Hi there... I'll give you my standard "speech" :) You really don't want to
"dim" stuff or to get to involved with "dimming". What you really want is
to understand variables and their attributes: type, value, lifetime and
visibility.

So when you declare a variable consider it's name and datatype but also how
long it will exist (given where you've declared it) and what else in the
application can access it. "As long as the program is running" infers you
have to declare it rather early on which (depending upon how you design your
apps) would be in the start-up procedure or the start-up form. I prefer to
define a class with Public Shared Sub Main() method because it gives me
added flexibility.

So I'd suggest you read up on the rules of variable scope and the various
keywords that allow you to define it; PUBLIC, PRIVATE, STATIC, FRIEND, DIM
along with SHARED. When you get a basic understanding of these keywords you
will instinctively know that (for instance) a variable declared in a dialog
box won't be available for the entire app because you declared it private to
that dialog box (visibility) and it won't exist (lifetime) before the dialog
box is created or after it is destroyed.

Similarly if you declare it PUBLIC in the main form it will exist when the
form is created and will exist (whether the form is visible or not) so long
as the form exists. It is accessible to the rest of the app (since it is
public) if the rest of the app can reference the instantiated form. If you
"Dim" a variable inside a method, inside a form, it's lifetime is short as
it is both created and destroyed within that method.
 
L

Linda Liu [MSFT]

Hi Cj,
Finally when that sub is finished how can I clean up or dispose of that instance I
created when I started the sub

What do you mean by 'clearn up or dispose' in the above sentence, destroy
the instance, or just release the resource that the instance uses but not
managed by CLR, e.g. Windows handles and database connections?

In .NET world, it's the garbage collector's responsibility to release and
destroy an object. In fact, we have no way to destroy an object manully or
actively in .NET world at all. After an object leaves scope, it is ready
to be destroyed by the garbage collector.

Before releasing object, the CLR automatically calls the Finalize method
for objects that define a Sub Finalize procedure. The Finalize method can
contain code that needs to execute just before an object is destroyed, such
as code for closing files and saving state information. There is a slilght
performance panalty for executing Sub Finalize, so you should define a Sub
Finalize method only when you need to release objects explicitly.

Objects are released more quickly when system resources are in short suppy,
and less frequently otherwise, which means that you cannot determine
exactly when the object will be destroyed.

To supplement garbage collection, your class can provide a mechanism to
actively manage system resources if they implement IDisposable interface.
IDisposable has one method, Dispose, which clients should call when they
finish using an object. You can use the Dispose method to immediately
release resources and perform tasks as closing files and database
connections.

In your paractice, if the referenced object uses some resource and you want
to release them, you could write code in the overrides Finalize method. At
the end of the function, you need to set the variable x to Nothing in order
to make the referenced object out of scope. Then the text step is to wait
for garbage collector to release and destroy the object. If you'd like to
release the resource the referenced object uses immediately, you could
implement IDisposable interface in your class and call the Dispose method
of the referenced object at the end of the function.

For more information on how objects are created and destroyed and how to
implement IDisposable interface, you may visit the following link.

" Object Lifetime: How Objects Are Created and Destroyed"
http://msdn2.microsoft.com/en-US/library/hks5e2k6.aspx

"Using Constructors and Destructors"
http://msdn2.microsoft.com/en-us/library/2z08e49e.aspx

Hope I made some clarification.

If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Phill W.

cj said:
Here's what I don't understand and really not sure how to ask but I want
to dim x as something when a program starts and this should be valid for
as long as the program is running.

Assuming your program lives within a single class:

Class Program

Private x As New Something
' Create an instance of a Something object.
' Store the reference to that object in variable x.

Public Shared Function Main( ByVal sArgs As String()) As Integer
. . .
End Sub

End Class
Then when I run a function I want it to create an instance using
x = new something.

No you don't - see below.
Finally when that sub is finished how can I clean up or dispose of
that instance I created when I started the sub - keep in mind I still
want the dim x as something I put at the start of the program to be
valid because I might want to rerun that sub again.

You're dealing with more than one /instance/ of the same /Type/ of
object. Consider the following:

Class Program

Private x As New Something
' The [poorly-named] variables x is available anywhere in this
' class, which probably means anywhere in your program, at
' least to start with.

Public Shared Function Main( ByVal sArgs As String()) As Integer

' Print a representation of the value held in x
Console.Writeline( x.ToString() )

ABC()

' Print a representation of the value held in x
Console.Writeline( x.ToString() )

XYZ()

' Print a representation of the value held in x
Console.Writeline( x.ToString() )

Return 0
End Sub

Private Sub XYZ()
Dim y as New Something
' This is a totally separate Something from x.
' They have the same Type - you could compare them - but
' they are separate variables and separate [new] object
' instances.
' y ONLY exists within this sub
End Sub

' So here, you could reference x, but not y, which is gone.

Private Sub ABC()
Dim z as New Something
' Same situation as y.
' This is a totally separate Something from x.
' z ONLY exists within this sub
End Sub

End Class

With the Framework's Garbage Collection, you don't [normally] need to
clean these objects up UNLESS they control un-Managed resources (graphic
objects are a typical case) in which case you should call their Dispose
method. Don't bother setting them to Nothing - this has no real effect
on Garbage Collection.

HTH,
Phill W.
 
C

cj

It sounds like cleanup is taken care of automatically and I really don't
have to do anything when I'm finished with an object. I thought I'd
heard I was supposed to call dispose. I just wanted to be sure I wasn't
supposed to be doing something like that. Thanks for the info.
 
L

Linda Liu [MSFT]

Hi Cj,

Thank you for your prompt response.

Yes, you're right. If you don't use unmanaged resources in the object, you
needn't call the Dispose method of the object (if available). All you need
to do is to set the object to Nothing and then wait the garbage collector
to release and destroy the object later.

If you have any other questions, please feel free to contact us. It's
always our pleasure to be of assistance.

Have a nice day!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
A

aaron.kempf

you can't dim X as something; you've got to dim it as an explicit TYPE

and everything then; everything is still an OBJECT.

does an INTEGER REALLY ****ING NEED INTELLISENSE?

eat SHIT microsoft

-Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top