vb.net variable

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???

LVL 8
bman9111Asked:
Who is Participating?
 
masoncooperConnect With a Mentor Commented:
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
 
bman9111Author Commented:
also forgot

if I have a

dim test() as string

how do I clear the results for that too???
0
 
lluthienCommented:
hmm
if you clear the variable,
you will still get all the messageboxes, displaying an empty box.


is that what you want?
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
bman9111Author Commented:
yup

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


0
 
lluthienCommented:
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
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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
 
bman9111Author Commented:
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
 
masoncooperCommented:
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
 
bman9111Author Commented:
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
 
lluthienCommented:
1> = nothing
2> that would depend on the scope of the variable.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.