0

I had the database Like this before

ItemName  Price

Against Item name I had the price in hashtable. some of my code is Like this

Hashtable pricesTilesBox = new Hashtable();
string  itemNameData=myReader["ItemName"].ToString().Trim();
int price=Convert.ToInt32(myReader["Price"]);
pricesTilesBox.Add(itemNameData,price);
foreach (string key in pricesTilesBox.Keys) 
{
 Console.WriteLine(key + '=' + pricesTilesBox[key]);
}

But now i have changed database table

ItemName  PriceLight PriceDark

So which data structure could be used now that i can get PriceLight PriceDark against itemName.because there are two prices now. Can hashtable be also used in this case?

Ali Nouman
  • 3,304
  • 9
  • 32
  • 53
  • This thread might help http://stackoverflow.com/questions/166089/what-is-c-sharp-analog-of-c-stdpair Make a Pair of the two _Price_ values and add the pair to the Hashtable – Bhargav Mangipudi Jan 22 '12 at 08:16

5 Answers5

1

What about using List<TileBox> ?

public class TileBox
{
 public string Name {get; set;}
 public decimal PriceLight {get; set;}
 public decimal PriceDark {get; set;}
}

List<TileBox> tileBoxes = new List<TileBox>();

//loop here to add TileBoxes to the List
{
 TileBox tileBox = new TileBox();
 tileBox.Name = myReader["ItemName"].ToString().Trim();
 tileBox.PriceLight = Convert.ToDecimal(myReader["PriceLight"]);
 tileBox.PriceDark = Convert.ToDecimal(myReader["PriceDark"]);
 tileBoxes.Add(tileBox);
}

This way also supports adding fields to TileBox later. You'd only need to change the class TileBox to hold the new field, and possibly the reader loop to read the field into the class, and the rest of your code can remain the same.

Bazzz
  • 26,427
  • 12
  • 52
  • 69
1

Why don't you create a class Price as :

public class Price
{
 public decimal PriceLight { get; set; }
 public decimal PriceDark  { get; set; }
}

Then use a Dictionary<string,Price>.

Mithrandir
  • 24,869
  • 6
  • 50
  • 66
1

If you still want to be able to use hashtable behavior of easily looking up an entry based on its name:

public class Entry {
  public string ItemName { get; set; }
  public int PriceLight { get; set; }
  public int PriceDark { get; set; }
}

Dictionary<string, Entry> pricesTilesBox = new Dictionary<string, Entry>();

string  itemNameData=myReader["ItemName"].ToString().Trim();
int light=Convert.ToInt32(myReader["PriceLight"]);
int dark=Convert.ToInt32(myReader["PriceDark"]);
pricesTilesBox.Add(itemNameData,new Entry { ItemName = itemNameData, PriceLight = light, PriceDark = dark });
foreach (string key in pricesTilesBox.Keys) 
{
 Console.WriteLine(key + '=' + pricesTilesBox[key]);
}
sblom
  • 26,911
  • 4
  • 71
  • 95
0

Well you can simply create a class for holding two items

MyClass
{
    PriceLight;
    PriceDark;
}

Use the same Hashtable but instead of inserting Price insert the object of MyClass against an ItemName.

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

If your properties might change again in the future, you might consider using Tuple class

http://msdn.microsoft.com/en-us/library/system.tuple.aspx

Arian
  • 135
  • 2
  • 11