Creating array for three items

S

Saga

Hello all,

I am reading 3 strings from a DB table. I need to apply the
same process to all three strings, so I figured that I would use
a for loop. To make the loop easier to handle I am going to
put the three strings into an array so I can loop through each
one, something like this:

dim sArr() as string

redim sArr(0)
sArr(0) = string 1

redim preserve sArr(1)
sArr(1) = string 2

redim preserve sArr(2)
sArr(2) = string 3

for each sItem as string in array
'process here
next

That seems simple enough, except that any one of the
strings might be empty. I could validate this within the loop:

for each sItem as string in array
if sItem.Length > 0 then
'process here
end if
next

Or I can create the array with only items that are not empty.
This method now needs more logic:

if string 1 <> "" then
redim sArr(0)
sArr(0) = string 1
end if

if string 2 <> "" then
redim preserve sArr(sArr.getupperbound(0) + 1)
sArr(sArr.getupperbound(0)) = string 2
end if

Same with string 3.

Is this the best way to do this? Any orientation or
references are welcomed! Thanks for your help. Saga
 
H

Herfried K. Wagner [MVP]

Am 02.04.2010 23:57, schrieb Saga:
I am reading 3 strings from a DB table. I need to apply the
same process to all three strings, so I figured that I would use
a for loop. To make the loop easier to handle I am going to
put the three strings into an array so I can loop through each
one, something like this:

dim sArr() as string

redim sArr(0)
sArr(0) = string 1

redim preserve sArr(1)
sArr(1) = string 2

redim preserve sArr(2)
sArr(2) = string 3

\\\
Dim sArr() As String = {string1, string2, string3}
///
for each sItem as string in array

Your array variable's name is 'sArr', not 'array'.
 
A

Appr3nt1c3

Hello all,

I am reading 3 strings from a DB table. I need to apply the
same process to all three strings, so I figured that I would use
a for loop. To make the loop easier to handle I am going to
put the three strings into an array so I can loop through each
one, something like this:

 dim sArr() as string

redim sArr(0)
sArr(0) = string 1

redim preserve sArr(1)
sArr(1) = string 2

redim preserve sArr(2)
sArr(2) = string 3

for each sItem as string in array
   'process here
next

That seems simple enough, except that any one of the
strings might be empty. I could validate this within the loop:

for each sItem as string in array
   if sItem.Length > 0 then
      'process here
   end if
next

Or I can create the array with only items that are not empty.
This method now needs more logic:

if string 1 <> "" then
  redim sArr(0)
  sArr(0) = string 1
end if

if string 2 <> "" then
  redim preserve sArr(sArr.getupperbound(0) + 1)
  sArr(sArr.getupperbound(0)) = string 2
end if

Same with string 3.

Is this the best way to do this? Any orientation or
references are welcomed! Thanks for your help. Saga

You said you are reading these string values from a DB table. Why not
just iterate using the datarows collection?

hth
 
C

Cor Ligthert[MVP]

Why not direct a datatable, probably the collection with the most simple
handling with all windows forms controls.

Cor
 
S

Saga

Thanks all for your replies!

Cor and Appr3nt1c3, I had not thought of using a datatable or datarows
collection for this because the strings came from 2 different tables and 2
of the strings came from one data row. I will look further into this.

Thank you all again. Saga
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top