0

i have a two dimentional array for items and prices in vb.net:

Dim items(,) As String = {{"Chicken Sand", "9000"}, {"Roast Beef Sand", "13500"}, 
                 {"Salmon Sand", "13500"}, {"Tuna Sand", "11000"},
                 {"Halloumi Sandwich", "7000"}, {"Ham & Cheese Sand", "8000"}, 
                 {"Water", "1500"}, {"Soft Drink", "2500"}, 
                 {"Fresh Lemonade", "3000"}, {"Fresh Orange", "3500"}}

I want to fill a combobox with items from this array and when each item from the combobox is selected I want that the price will be displayed in a textbox specific for the price. I reached:

For row = 0 To items.GetUpperBound(0)
    cbitem.Items.Add(items(row, 0))
Next`
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Emile Arab
  • 61
  • 10
  • You can use a utility class or an Anonymous type to create a DataSource as the linked answers show – Ňɏssa Pøngjǣrdenlarp Dec 21 '16 at 00:31
  • @Plutonix i didn't understand – Emile Arab Dec 21 '16 at 00:41
  • If you want to display one thing to the user and collect something else like a value, you should use a datasource. It can be a datatable or list(of T) or a list of anonymous types. The linked post shows how as does: http://stackoverflow.com/a/39024484/1070452 It will also let you store the price as a value rather than text – Ňɏssa Pøngjǣrdenlarp Dec 21 '16 at 00:47
  • @Plutonix there is no way to use a two dimentional area ?? – Emile Arab Dec 21 '16 at 00:48
  • You arent really using a 2D array - you are iterating it to extract the data one by one anyway. Might as well do it correctly – Ňɏssa Pøngjǣrdenlarp Dec 21 '16 at 00:50
  • Those things have 2 properties: a name and a price stored as string. Why store them seperately when they can be together and of the correct data type? – Ňɏssa Pøngjǣrdenlarp Dec 21 '16 at 00:52
  • yes got it. but this is a homework and we were asked to fill the combobox with items by a two dimentional array. this is why i am asking you if it can work. – Emile Arab Dec 21 '16 at 01:02
  • @Plutonix i forgot to tag you in the previous comment – Emile Arab Dec 21 '16 at 01:03
  • You only need to ping (`@`+name) when more than one person has commented. The items collection can hold anything but each can only store one thing - you want to store 2 things. If you glue the elements together (`"Tuna Sand (11000)"`) you could parse it back out when they select, but it is a wet bag of hair compared to using a DataSource – Ňɏssa Pøngjǣrdenlarp Dec 21 '16 at 01:11

1 Answers1

2

this is a homework and we were asked to fill the combobox with items by a two dimentional array

This is not a good way to do this compared to using a datasource:

You cant actually put your array into the ComboBox because Items is a collection, not a multi-D array. But you can use it as a look up for what was selected. Be sure your array has form level scope:

Option Strict On
Public Class MenuFrm

    Private items(,) As String = {{"Chicken Sand", "9000"}, {"Roast Beef Sand", "13500"},
                    {"Salmon Sand", "13500"}, {"Tuna Sand", "11000"},
                    {"Halloumi Sandwich", "7000"}, {"Ham & Cheese Sand", "8000"},
                    {"Water", "1500"}, {"Soft Drink", "2500"},
                    {"Fresh Lemonade", "3000"}, {"Fresh Orange", "3500"}}

Later, perhaps in form load, fill the cbo with the names:

For row = 0 To items.GetUpperBound(0)
    cbox1.Items.Add(items(row, 0))
Next

Now, when they pick something look up the string price:

Private Sub cbox1_SelectedIndexChanged1(sender As Object, 
        e As EventArgs) Handles cbox1.SelectedIndexChanged
    If cbox1.SelectedIndex < 0 Then Return
    Dim price = Convert.ToDecimal(items(cbox1.SelectedIndex, 1))
End Sub

A proper DataSource would store the price as a numeric, keep the price with the menu item and make it all available in the SelectedValueChanged.

Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • thank you so much @plutonix i will use the 2d array since this is what was asked from me. And thank you for the DataSource way it is better. – Emile Arab Dec 21 '16 at 01:35