albert albert

albert albert

  • NA
  • 524
  • 0

Problems with array

Nov 8 2011 4:22 PM
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)