Search array contents and display

adi_uk

Member
Joined
Mar 22, 2006
Messages
12
Programming Experience
1-3
I have the following code where im trying to set it so that when a user enters an artist into an input box, only the records that match that artist are shown. Im having a problem, where by, it will output the data as many times as thee are records. I guess this is since it is not moving to the next record, and so outptting the same data.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] input [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aCD [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] CatalogueCD
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] selectedCD [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]input = InputBox("Enter Artist Name", "CD Input Box", "", 1, 1)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] element [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]element = 1
ListBox1.Items.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] (element <= index)
aCD = CDList(selectedCD)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] aCD.CDArtist = input [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ListBox1.Items.Add("Title: " & aCD.CDTitle & " by " & aCD.CDArtist)
element = element + 1
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]element = element + 1
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Loop
[/COLOR][/SIZE]
 
'element' is the variable that you're incrementing but you aren't using it to get a different object each time through the loop. Your using 'selectedCD' to get an object from CDList but 'selectedCD' doesn't change over the course of the loop. You don't need 'element' and 'selectedCD'. Also, you should be using a For or For Each loop if you want to loop a finite number of times. You should read the help/MSDN topics for the For...Next statement and the For Each... Next statement to see how they differ and how each should be used. Do and While loops are generally only used if you don't know how many times the loop will be executed.
VB.NET:
For Each cd As CatalogueCD In CDList
    If cd.CDArtist = input Then
        'We have a match.
    End If
Next cd
 
Thank you for your reply. Guess I have lots of learning to do yet, but im getting there slowly.

I have the following, although i get an error as shown below.

VB.NET:
[I][SIZE=2][COLOR=#800080]
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] input [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]input = InputBox("Enter Artist Name", "CD Input Box", "", 1, 1)
ListBox1.Items.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] aCD [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] CatalogueCD [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] CDList
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] aCD.CDArtist = input [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ListBox1.Items.Add("helo")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] aCD
[/SIZE][/I][/COLOR][/SIZE]

The error was:

VB.NET:
An unhandled exception of type 'System.NullReferenceException' occurred in WirelessSession2.exe

And it was this line

VB.NET:
[SIZE=2][COLOR=#0000ff]
If[/COLOR][/SIZE][SIZE=2] aCD.CDArtist = input [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE]
 
Back
Top