Skip to content

Instantly share code, notes, and snippets.

@ugraphix
Created January 13, 2017 04:02
Show Gist options
  • Save ugraphix/fc2b0e81a201e3ca358556b6c06dd071 to your computer and use it in GitHub Desktop.
Save ugraphix/fc2b0e81a201e3ca358556b6c06dd071 to your computer and use it in GitHub Desktop.
Playing with arrays using c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
string[,] product = new string[5, 2]; //two dimensional array with 5 rows, 2 collumns
protected void Page_Load(object sender, EventArgs e) //create list of items
{
product[0, 0] = "Lettuce";
product[0, 1] = ".99;";
product[1, 0] = "carrots";
product[1, 1] = "2.98";
product[2, 0] = "Apple";
product[2, 1] = ".68";
product[3, 0] = "Tomato";
product[3, 1] = ".80";
product[4, 0] = "Celery";
product[4, 1] = "1.10";
if (!IsPostBack)
{
GetProduce();
}
}
//method
protected void GetProduce()
{
for (int i = 0; i < 5; i++) //++ incrementing by 1
{
ListItem item = new ListItem();
item.Text = product[i, 0]; //add text to array
ProduceCheckbox.Items.Add(item); //add items
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int counter = 0;
double total = 0;
foreach (ListItem item in ProduceCheckbox.Items)
{
//string productName = product[counter, 0];
if (item.Selected)
{
if (item.Text.Equals(product[counter, 0]))
{
total += double.Parse(product[counter, 5]);
}
}
counter++;
}
Label2.Text = "Your Total is " + total.ToString("C");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment