How to search an arraylist for a string and then retrieve the index?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Can anyone tell me how i can search an arraylist to find out if it contains a specific string??
i.e. the user selects a mealName from a combobox, they then enter a meal quantity. The user then presses the add button and the mealname and mealQTY is pushed to an MealNameArrayList and a MealQtyArrayList. If the mealName already exists in the MealNameArrayList i want to retrieve the index of the mealname from the arraylist so that i can use it to add the new mealqty to the previous MealQty for that meal type in the MealQtyArrayList.

Does anyone know how i can do this??
 
Hi Mattias

I tried this method but i still keep getting error'

This is the error that i get
An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dl
Additional information: Public member 'add' on type 'String' not found

This is my code i tried
Dim ChosenMeal As Strin
ChosenMeal = CBoxMealName.Text.ToStrin
Dim Quantity As Intege
Quantity = CInt(txtMealQty.Text
Dim found As Boolea
Dim MealTypeindex As Intege
Dim ExistingMealQty As Intege

MealTypeIndex = MealTypeArrayList.IndexOf(ChosenMeal
ExistingMealQty = MealQtyArrayList(MealTypeindex
ExistingMealQty = ExistingMealQty + Quantit
MealTypeArrayList.RemoveAt(MealTypeindex
MealTypeArrayList(MealTypeindex).add(ExistingMealQty

What im doin here is that im searchin my mealType Arraylist to see if the meal has already been previously chosen. If it does exist in this array i am retrieveing the index of this

I then want to use this Index in my Meal Qty Arraylist so that i can add the new meal qty to the previously existing qty
I think that am i getting errors in this addition part but i cannot see where im goin wrong

Can anyone help
 
That's because MealTypeArrayList(MealTypeindex) is a string. The last
line should be something like
MealTypeArrayList.insert(MealTypeindex, ExistingMealQty)
 
Hi

I corrected this. I was adding to the incorrect arraylist!! I still get an error though...

This is the error that i get
An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dl
Additional information: Public member 'add' on type 'Integer' not found

This is my code i tried
Dim ChosenMeal As Strin
ChosenMeal = CBoxMealName.Text.ToStrin
Dim Quantity As Intege
Quantity = CInt(txtMealQty.Text
Dim MealTypeindex As Intege
Dim ExistingMealQty As Intege

MealTypeIndex = MealTypeArrayList.IndexOf(ChosenMeal
ExistingMealQty = MealQtyArrayList(MealTypeindex
ExistingMealQty = ExistingMealQty + Quantit
MealQtyArrayList.RemoveAt(MealTypeindex
MealQtyArrayList(MealTypeindex).add(ExistingMealQty)
 
This is the error that i get:
An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dll
Additional information: Public member 'add' on type 'Integer' not found.

Do yourself a favor and turn on Option Strict.

MealQtyArrayList(MealTypeindex).add(ExistingMealQty)

I'm guessing that you actually want

MealQtyArrayList.Insert(MealTypeIndex, ExistingMealQty)



Mattias
 
Back
Top