[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vb.net variable

Posted on 2005-04-21
10
Medium Priority
?
224 Views
Last Modified: 2010-04-17
What is the best way to clear a variable


ex.

dim v as string
for i = 1 to 10 - 1
if textbox1.text =  "1" then
      v = "One"
end if
msgbox v
'here I want to clear the variable because if v = One anytime from here on out when the code stops on the msgbox it will always say One.
next

so should I do
v = ""
or v = nothing

what is the best way to clear a variable???

0
Comment
Question by:bman9111
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Author Comment

by:bman9111
ID: 13833203
also forgot

if I have a

dim test() as string

how do I clear the results for that too???
0
 
LVL 11

Expert Comment

by:lluthien
ID: 13833348
hmm
if you clear the variable,
you will still get all the messageboxes, displaying an empty box.


is that what you want?
0
 
LVL 8

Author Comment

by:bman9111
ID: 13833743
yup

I think I figured everything out, Unless u think there is a better way to clear the variable. Part 2 is done.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:lluthien
ID: 13833847
if you set the variable to '' you still have a string.

if you set it to nothing, that is basically what you have..
- nothing

and that is what you want,

it basically looks like an unitialized variable then.





0
 
LVL 2

Accepted Solution

by:
masoncooper earned 600 total points
ID: 13834327
Try changing the scope of v
--------From:-------------
dim v as string
for i = 1 to 10 - 1
if textbox1.text =  "1" then
      v = "One"
end if
msgbox v
'here I want to clear the variable because if v = One anytime from here on out when the code stops on the msgbox it will always say One.
next
---------to--------
for i = 1 to 10 - 1
dim v as string
if textbox1.text =  "1" then
      v = "One"
end if
msgbox v
'here I want to clear the variable because if v = One anytime from here on out when the code stops on the msgbox it will always say One.
next
-------------------
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 13834386
When you "clear" a variable, you have two basic options:

(1) Use a sentinel value.  A sentinel value is one that would never be valid for use in your program, but is still valid for that variable type.  If you have an number variable then perhaps you could set it to -1.  This all depends on how your app works.

(2) Revert the variable to an uninitialized state.  This is not possible for all variable types.  For instance, it is possible to set a string variable to Nothing, but not an Integer:

        ' This works for a string...
        Dim v As String
        v = ""
        If v <> "" Then
            MsgBox(v)
        End If
        v = Nothing
        If Not (v Is Nothing) Then
            MsgBox(v)
        End If

        ' ...but not for an integer
        Dim i As Integer
        i = 1
        MsgBox(i)
        i = Nothing ' this compiles and runs...BUT i becomes 0 (zero), NOT Nothing!!!
        MsgBox(i)

So in answer to your question, "what is the best way to clear a variable???", that just really depends on the type of variable, how your application is using it, and the possible valid values that variable can have in your program.
0
 
LVL 8

Author Comment

by:bman9111
ID: 13839002
so If I use = nothing then the variable isn't taken any memory

what about if I use i = "" is the variable out of memory or not???
0
 
LVL 2

Expert Comment

by:masoncooper
ID: 13842203
bman9111:
I'm going to refer back to C++ because its easier to explain in this case
Lets say you have a variable (character array), at the moment, it takes up 0 bytes.  Now if you set the myvar=""; the variable takes up one byte because something you don't see is the null (\0 or chr(0) for VB ) character is added to signify the end of the line.
VB does not necessarily behave like this, it allocates a more complex object, but the point still exists, i=nothing doesn't create the object and i="" allocates an object with no text.

Personally, I wouldn't worry about saving memory right now, your program here is fairly simple and you'd be suprised at the size of some other variables.

Going back to your original question, I didn't make myself too clear earlier
Here's your code again
dim v as string
for i = 1 to 10 - 1
if textbox1.text =  "1" then
      v = "One"
end if
msgbox v
'here I want to clear the variable because if v = One anytime from here on out when the code stops on the msgbox it will always say One.
next
-------------------
Since v is only accessed within the for loop and used only during that one loop instance, moving the variable within the for loop limits its scope so that it only exists during that one instance of the loop.  By properly scoping the variable you eliminate the need to clear out the variable between loops and effectively save memory at the same time because v isn't accessible outside the for loop.

Since
0
 
LVL 8

Author Comment

by:bman9111
ID: 13902971
so I guess the real question is once a variable as been used, what is the best way to clear it.

meaning

if a button on the form is press and
Dim i as string
i = "this is a test"

how would u clear that variable. also if the form is closed is the variable still there.???
0
 
LVL 11

Expert Comment

by:lluthien
ID: 13903389
1> = nothing
2> that would depend on the scope of the variable.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question