Android Question Clear String

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

In my Globals sub I have declared a string like:

B4X:
Dim MyString(200) as String

I am then setting the string from another sub like:

B4X:
MyString(1) = "one"
MyString(2) = "two"
MyString(3) = "three"

If I want to clear all the strings, what would be the best way in doing that?

Would I need to use a For loop like below or is there a better faster way?

B4X:
For i = 1 to 200
    MyString(i) = ""
Next
 

udg

Expert
Licensed User
Longtime User
The fastest way should be to dim the array again, so:
B4X:
Dim MyStrings(200) as String
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Be careful:
With Dim MyString(200) As String
and For i = 1 To 200
you will get an error !
You should use either For i = 0 To 199
or Dim MyString(201) As String if you really want the indexes from 1 to 200, the index 0 wouldn't be used in this case.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
The fastest way should be to dim the array again, so:
B4X:
Dim MyStrings(200) as String
But How?

I am Dimming MyString in the Globals sub.

If I was to re-dim the MyString it would only be accessible from that sub and not the rest of my code.

@klaus The code I provided was only a example so people know what I am trying to do.. I actually have it set as what you said. (I had made a mistake with the code I posted.. Whops..)
I will use the For loop to clear it as I think this is the only way in clearing the strings.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If I was to re-dim the MyString it would only be accessible from that sub and not the rest of my code.
This is not true, if you re-dim a global variable, it is still a global variable. Just reset.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
This is not true, if you re-dim a global variable, it is still a global variable. Just reset.
Umm.. So you are right.

I always thought if you dim something it was only accessible in that sub. But looks like if you dim it again in a sub, and it's in the Globals or Process_Globals subs it looks like it re-dims it again, so yes in my case clears the strings.

You learn something all the time.
Thanks for confirming this, and looks like I can just re-dim it again which will make it easy for me.
 
Upvote 0
Top