Home All Groups Group Topic Archive Search About

Creating array for three items

Author
2 Apr 2010 9:57 PM
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

Author
3 Apr 2010 12:10 AM
Herfried K. Wagner [MVP]
Am 02.04.2010 23:57, schrieb Saga:
Show quoteHide quote
> 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'.

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

--
  M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
3 Apr 2010 11:42 AM
Appr3nt1c3
Show quote Hide quote
On 3 Abr, 05:57, "Saga" <antiS***@nowhere.com> wrote:
> 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
Author
3 Apr 2010 5:03 PM
Cor Ligthert[MVP]
Why not direct a datatable, probably the collection with the most simple
handling with all windows forms controls.

Cor

Show quoteHide quote
"Saga" <antiSpam@nowhere.com> wrote in message
news:uWtvo9q0KHA.840@TK2MSFTNGP06.phx.gbl...
> 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
>
>
Author
5 Apr 2010 4:50 PM
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