populating an array programatically

Results 1 to 2 of 2

Thread: populating an array programatically

  1. #1
    Join Date
    Dec 1969

    Default populating an array programatically

    I have a search form that uses 3 listbox controls to allow users to select multiple option from 3 different categories. the 3 listboxes are each populated from the same underlying table in a sql server database, but each list box uses a different filter to pull up a different subset of the rows in the same underlying sql server table.<BR><BR>my problem is coming when i try to consolidate the selected items from the 3 listbox controls into one single array object. i need the selection criteria from the 3 listboxes to be in one single array object in order to keep my sql manageable. this is already a complex search form, and it would be way too much additional complexity to have the code manually check each listbox every time it needs to construct a sql statement.<BR><BR>the error message that i am getting is:<BR>-----------------------------------------------------------------<BR>Server Error in &#039;/&#039; Application.<BR><BR>Compilation Error <BR>Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. <BR><BR>Compiler Error Message: BC30452: Operator &#039;&&#039; is not defined for types &#039;1-dimensional array of String&#039; and &#039;String&#039;.<BR><BR>Source Error:<BR><BR>Line 118: If MyYogaListBox.Items(i).Selected Then <BR>Line 119: Message.Text = Message.Text & MyYogaListBox.Items(i).Text & "<BR>" <BR>Line 120: practices = practices & MyYogaListBox.Items(i).Text & "," <BR>Line 121: End If <BR>Line 122: Next <BR><BR>Source File: E:artisanchef.compracticecommunitysearch10-2002.aspx Line: 120 <BR><BR>Show Detailed Compiler Output:<BR><BR>E:artisanchef.compracticecommunitys earch10-2002.aspx(120) : error BC30452: Operator &#039;&&#039; is not defined for types &#039;1-dimensional array of String&#039; and &#039;String&#039;.<BR><BR> practices = practices & MyYogaListBox.Items(i).Text & "," <BR> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <BR>E:artisanchef.compracticecommunitysearch10-2002.aspx(126) : error BC30452: Operator &#039;&&#039; is not defined for types &#039;1-dimensional array of String&#039; and &#039;String&#039;.<BR><BR> practices = practices & MyMeditationListBox.Items(i).Text & ","<BR> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <BR>E:artisanchef.compracticecommunitysearch10-2002.aspx(132) : error BC30452: Operator &#039;&&#039; is not defined for types &#039;1-dimensional array of String&#039; and &#039;String&#039;.<BR><BR> practices = practices & MyTransferenceListBox.Items(i).Text & ","<BR> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <BR>E:artisanchef.compracticecommunitysearch10-2002.aspx(138) : error BC30337: &#039;For&#039; loop control variable cannot be of type &#039;String&#039;.<BR> For nnum = 0 To UBound(practices)<BR> ~~~~ <BR>-----------------------------------------------------------------<BR>the section of code that generates the error is as follows:<BR>-----------------------------------------------------------------<BR> Dim practices() As String<BR> &#039; check which options are selected<BR> Message.Text = "Selected Item(s):<BR><BR>" <BR> Dim i As Integer <BR> For i = 0 To MyYogaListBox.Items.Count - 1 <BR> If MyYogaListBox.Items(i).Selected Then <BR> Message.Text = Message.Text & MyYogaListBox.Items(i).Text & "<BR>" <BR> practices = practices & MyYogaListBox.Items(i).Text & "," <BR> End If <BR> Next<BR> For i = 0 To MyMeditationListBox.Items.Count - 1 <BR> If MyMeditationListBox.Items(i).Selected Then <BR> Message.Text = Message.Text & MyMeditationListBox.Items(i).Text & "<BR>"<BR> practices = practices & MyMeditationListBox.Items(i).Text & ","<BR> End If <BR> Next<BR> For i = 0 To MyTransferenceListBox.Items.Count - 1 <BR> If MyTransferenceListBox.Items(i).Selected Then <BR> Message.Text = Message.Text & MyTransferenceListBox.Items(i).Text & "<BR>"<BR> practices = practices & MyTransferenceListBox.Items(i).Text & ","<BR> End If <BR> Next<BR><BR> Message2.Text = "<BR>&#060;hr&#062;Selected Item(s) from string:<BR><BR>" <BR> Dim nnum As String<BR> For nnum = 0 To UBound(practices)<BR> Message2.Text = Message.Text & practices(nnum) & "<BR>" <BR> Next<BR> &#039; end of check<BR>-----------------------------------------------------------------<BR>i cant find any documentation at the msdn site for the string object, or for any array object. i am assuming from a fragment of a suggestion from someone the other day that arrays are made by using special syntax with a string object. this is what i am trying to do. and it seems perhaps like it might be working except that i have to figure out something else to use instead of &. basically, i need to figure out how to populate an array programatically.<BR><BR>can anyone show me how to accomplish this task without getting the error message that i posted above?

  2. #2
    Join Date
    Dec 1969

    Default RE: populating an array programatically

    &#062; populating an array programatically<BR><BR>Is there any other way? :-)<BR><BR><BR>You cannot add items to an array by using the & string concatenation operator. You assign a string to the indexed element of an array like so:<BR><BR> myArray(elementNumber) = Value<BR><BR>In VB.NET you can use Redim Preserve to alter the size of the last dimension of an array:<BR> <BR> &#039; add an extra element<BR> Redim Preserve practices(practices.GetUpperBound(0) + 1)<BR><BR> &#039; add that value to the last element<BR> practices(practices.GetUpperBound(0)) = MyMeditationListBox.Items(i).Text & ","<BR><BR><BR>However, rather than incurring the resizing overhead for every element that you want to add to the array, it&#039;s probably more efficient to declare your initial array with ample element *slots* and then resize "down" at the end. A bit like this:<BR><BR> Dim practices(100) As String<BR> Dim counter As Int32 = 0<BR><BR><BR> &#039; Add an element...<BR> practices(counter) = MyMeditationListBox.Items(i).Text & ","<BR> &#039; Increase the counter...<BR> counter += 1<BR><BR> &#039; ...add more elements...<BR><BR> &#039; at the end, resize the array size to the number of elements that you&#039;ve used<BR> Redim Preserve practices(counter)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts