[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

VB - ARRAYS

i hv a problem see whether u could solve this
I hv an Array  
dim sMyArray(100) as string
i hv to reinitilize this every time for some purpose
i.e putting this in a for loop
for i=0 to 100
sMyArray(i) = 0
next i
check it out is there any other way for this
0
chandu_kappa
Asked:
chandu_kappa
  • 4
  • 3
1 Solution
 
TimCotteeCommented:
Firstly why are you assigning a value of 0 to an array of strings.

If you have defined a fixed array of strings then the value of each element is a zero length string. If you want the value of sMyArray's elements to be zero then use an integer type.

dim sMyArray(100) as Integer

will give you what you want.
0
 
grumpy147Commented:
Hi,

The array is defined as a string - does it have to be initialised to the empty string ("") or to a string containing zero ("0") as indicated by your code.

If the former, try the following

Dim myArray() as string

.....
'wher you need to initialise the array, just redim it
Redim myArray(100)


Hope this helps
0
 
chandu_kappaAuthor Commented:
no i don't wana to redim the array just the value inside to be initilized
the situation is like this

declared an array as integer
 
dim iMyArray(100) as integer
dim i as integer
i hv to reinitilize this every time
i.e putting this in a for loop

for i=0 to 100
sMyArray(i) = 0
next i

check it out is there any other way ,to initialize other than FOR loop.i dont want to use loop.

get back immediately
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
TimCotteeCommented:
The array elements should already be initialised to zero. Try the following to prove this.

Create a form with a command button.

Dim iMyArray(100) As Integer

Private Sub Command1_Click()
    intElement = Int(Rnd() * 100 + 1)
    MsgBox (intElement & " = " & iMyArray(intElement))
End Sub

Each time you click the command button it displays the number and value of a randomly chosen element in your array. As you will see, without any loop they are already initialised to zero.


0
 
chandu_kappaAuthor Commented:
i agree that
but i hv set it to some value
this particular array is in some function
and this function is repeatedly called
so instead of putting a big for loop i wanna in a shorter way


get back soon
0
 
TimCotteeCommented:
Then I think that you have encountered a "limitation" of VB. The only thing that I can suggest to help with this then is to move the array definition outside of the function. If you make the array public or at least private to the form then it can be initialised once only and used within one or more functions within the scope of the declaration.
0
 
chandu_kappaAuthor Commented:
ok then.thanks a lot
0
 
chandu_kappaAuthor Commented:
i agree that
but i hv set it to some value
this particular array is in some function
and this function is repeatedly called
so instead of putting a big for loop i wanna in a shorter way


get back soon
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now