From: Saga on
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


From: Herfried K. Wagner [MVP] on
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'.

> 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/>
From: Appr3nt1c3 on
On 3 Abr, 05:57, "Saga" <antiS...(a)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
From: Cor Ligthert[MVP] on
Why not direct a datatable, probably the collection with the most simple
handling with all windows forms controls.

Cor

"Saga" <antiSpam(a)nowhere.com> wrote in message
news:uWtvo9q0KHA.840(a)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
>
>
From: Saga on
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





 | 
Pages: 1
Prev: forms control in a class
Next: Create a template.