4
Answers

Problems with array

albert albert

albert albert

13y
1.2k
1
Hi Everyone,

I have this:

[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VendingMachineApp
{
  public partial class Form1 : Form
  {
  VendorMachine vendorMachine;
  public String[] snacks = { "Cookie", "Bubble gum", "Prefzels", "Soda" };
  string line = "has been dispended";
  int choose;
  int i;
  public Form1()
  {
  InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
  string[] snacks = new string[4];
  snacks[0] = "Cookie";
  snacks[1] = "gum";
  snacks[2] = "Prefzels";
  snacks[3] = "soda";
 
 
  switch (snacks[choose])
  {
  case snacks[0]  :txtOutput.Text =  snacks[0] + line;
  break;
  case snacks[1]: txtOutput.Text = snacks[1] + line;
  break;
  case snacks[2]: txtOutput.Text = snacks[2] + line;
  break;
  case snacks[3]: txtOutput.Text = snacks[3] + line;
  break;
  default:
  break;
  }//end swicth
 

 

  }
  }
}

[/code]

but what has to be for the case? case snacks[1] - then I get an error: "A constant value is expected".

I just want if user put an 1 in a textbox that u get as outcome: "gum" and for 2 - outcome: "prefzels".

THX
Answers (4)