PC Review


Reply
Thread Tools Rate Thread

Creating array for three items

 
 
Saga
Guest
Posts: n/a
 
      2nd Apr 2010
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


 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      3rd Apr 2010
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/>
 
Reply With Quote
 
Appr3nt1c3
Guest
Posts: n/a
 
      3rd Apr 2010
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
 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      3rd Apr 2010
Why not direct a datatable, probably the collection with the most simple
handling with all windows forms controls.

Cor

"Saga" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>

 
Reply With Quote
 
Saga
Guest
Posts: n/a
 
      5th Apr 2010
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





 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
find each of the items in an array and save result in another array lif Microsoft Excel Programming 2 28th Jun 2006 01:54 AM
WHY OH WHY! ... creating a dynamic array of outlookmail items, then display them. WhytheQ Microsoft Excel Programming 4 24th Jun 2006 09:43 AM
Creating an Array from a multidimensional array @ nospam.com Microsoft C# .NET 1 20th Aug 2003 12:13 PM
Re: How to add items in Array Shyam Microsoft Powerpoint 0 21st Jul 2003 04:08 PM
Re: How to add items in Array Chirag Microsoft Powerpoint 0 21st Jul 2003 08:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:51 AM.