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

dim x as something

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.
Dec 12 '06 #1
7 1624
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.

"cj" <cj@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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.

Dec 12 '06 #2
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.
"cj" <cj@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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.

Dec 12 '06 #3
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Dec 13 '06 #4
cj wrote:
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.
Dec 13 '06 #5
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.
Linda Liu [MSFT] wrote:
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Dec 13 '06 #6
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

Dec 14 '06 #7
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 FUCKING NEED INTELLISENSE?

eat SHIT microsoft

-Aaron

cj wrote:
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.
Dec 14 '06 #8

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

Similar topics

3
by: Martin Lucas-Smith | last post by:
I am trying to take a string and split it into a filename and a number where the number is what follows the *last* instance of a comma in that string. Split and explode won't work because if the...
8
by: Sam Sungshik Kong | last post by:
Hello! I use Python for ASP programming. I found something weird. Response.Write(Request("something")) It draws "None" when there's no value for something. Actually I expect "" instead of...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
7
by: Zero | last post by:
If we have a structure like: struct something{ int *a; int b; }; We allocate mempry for a using malloc or calloc. The question is when we want to know the size of the structure,...
44
by: Tolga | last post by:
As far as I know, Perl is known as "there are many ways to do something" and Python is known as "there is only one way". Could you please explain this? How is this possible and is it *really* a...
5
by: Daniel Vukadinovic | last post by:
Can anyone explain me these things in C++? 1.What is :: used for like in the next case: if(.... { ... ::one;
4
by: D.Hering | last post by:
When I see something like this prototype: SOMETHING int func( int x ); what is SOMETHING doing there? thanks very much, -Dieter
2
by: Andrew Backer | last post by:
I hope someone out there can help me with this, because I am stuck. My problem is that I have an instance of a generic class, but I don't know ahead of time what the type parameter used was. In...
5
by: Ayebaleet | last post by:
Hello everybody, I understand the basics of Java, but not such features as are helpful in an applet namely Listeners. I wanted to create a feature like this: A program would be displayed in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.