Reputation: 3414
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?
Upvotes: 0
Views: 102
Reputation: 135
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
Upvotes: 0
Reputation: 26922
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.
Upvotes: 1
Reputation: 27353
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]);
}
Upvotes: 1
Reputation: 25377
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>
.
Upvotes: 1
Reputation: 30097
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
.
Upvotes: 0