我还没有足够的知识能够应用我发现的类似问题的其他解决方案,所以如果有人认为我正在复制那么道歉。
我对c#很新,并在windows窗体中构建一个简单的交易游戏。
简而言之,当单击dgv中包含该对象名称的单元格时,我想要返回列表对象的Description属性。顺便说一下,这份名单是单独的公共课。
这是我到目前为止所做的:
public class Commodity
{
public int ID { get; set; }
public int Cost { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsPerishable { get; set; }
public bool IsIllegal { get; set; }
public Commodity (int id, int cost, string name, string description, bool isPerishable, bool isIllegal)
{
ID = id;
Cost = cost;
Name = name;
Description = description;
IsPerishable = isPerishable;
IsIllegal = isIllegal;
}
}
...
public static class World
{
public static readonly List<Commodity> Commodities = new List<Commodity>();
...
public const int COMMODITY_ID_TEA = 1;
public const int COMMODITY_ID_SPICES = 2;
public const int COMMODITY_ID_FISH = 3;
public const int COMMODITY_ID_SILK = 4;
public const int COMMODITY_ID_SLAVES = 5;
public const int COMMODITY_ID_NARCOTICS = 6;
public const int COMMODITY_ID_FRUIT = 7;
public const int COMMODITY_ID_MEAT = 8;
public const int COMMODITY_ID_BOOKS = 9;
public const int COMMODITY_ID_METAL= 10;
public const int COMMODITY_ID_COAL = 11;
public const int COMMODITY_ID_WEAPONS = 12;
public const int COMMODITY_ID_SUGAR_CANE= 13;
public const int COMMODITY_ID_MEDICINE = 14;
public const int COMMODITY_ID_DEEDS = 15;
...
static World()
{
PopulateCommodities()
}
...
private static void PopulateCommodities()
{
Commodities.Add(new Commodity(COMMODITY_ID_TEA, 25, "Tea", "Dried leaves that get boiled then drunk.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_SPICES, 50, "Spices", "Individually wrapped though, not a secret blend of 13.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_FISH, 35, "Fish", "Smells like fish.", true, false));
Commodities.Add(new Commodity(COMMODITY_ID_SILK, 120, "Silk", "Luxurious material that makes really comfy pants.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_SLAVES, 800, "Slaves", "Who doesn't want their own slave? Careful though, it's against the law to trade these in some places.", false, true));
Commodities.Add(new Commodity(COMMODITY_ID_NARCOTICS, 2000, "Narcotics", "Substances that are dangerous and illegal - don't get caught buying or selling!", false, true));
Commodities.Add(new Commodity(COMMODITY_ID_FRUIT, 400, "Fruit", "Exotic fruits from all over the world. Good in smoothies.", true, false));
Commodities.Add(new Commodity(COMMODITY_ID_MEAT, 750, "Meat", "Cured meat. Cured from what, though?", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_BOOKS, 1250, "Books", "First edition collectibles. Don't get them wet.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_METAL, 800, "Metal", "Construction materials. For building with.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_COAL, 1000, "Coal", "Black rocks that burn really well.", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_WEAPONS, 5000, "Weapons", "Guns and bombs and stuff. Illegal in some places.", false, true));
Commodities.Add(new Commodity(COMMODITY_ID_SUGAR_CANE, 1500, "Sugar Cane", "Sweet!", true, false));
Commodities.Add(new Commodity(COMMODITY_ID_MEDICINE, 7500, "Medicine", "Maybe these cured the meat?", false, false));
Commodities.Add(new Commodity(COMMODITY_ID_DEEDS, 15000, "Deeds", "Title deeds to some prime land!", false, false));
}
...
public static Commodity CommodityByID(int id)
{
foreach(Commodity commodity in Commodities)
{
if(commodity.ID == id)
{
return commodity;
}
}
return null;
}
......这就是信息的构建方式。
public partial class Plunder : Form
{
... // The dgv is populated at this point.
private void dgvShopStock_CellClick(object sender, DataGridViewCellEventArgs e)
{
string chosenCell = dgvShopStock.SelectedCells[0].Value.ToString();
string chosenType = dgvShopStock.Columns[0].Name.ToString();
//So far so good - chosenCell returns the contents and chosenType returns the column header as there are other types of object which can appear in this box - not important now.
//Here's me trying IndexOf. Returns a 'cannot convert string to Commodity' error. I tried casting without success.
int index = World.Commodities.IndexOf(chosenCell);
//This attempts to construct the strings "World.CommodityByID" and the parameter for it of "World.COMMODITY_ID_BOOKS" in this case. This was to attempt reflection instead but I dont fully understand it and don't know where to go from here.
string methodName = "World." + chosenType + "ByID";
string param = "World." + chosenType.ToUpper() + "_ID_" + chosenCell.ToUpper();
Commodity returnedItem;
MethodInfo chosenItem = this.GetType().GetMethod(methodName);
chosenItem.Invoke(this, param);
rtbDescription.Text = returnedItem.Description;
}
}
很抱歉这个问题很长,但是我不善于从局外人的眼中解释我的情况,所以我认为最好包含相关的代码。
由于
答案 0 :(得分:0)
当我提出错误的问题时,这不是我的问题的答案,但这是我为达到预期结果所做的。
foreach (Commodity commodity in World.Commodities)
{
if (chosenCell == commodity.Name)
{
rtb.Description = commodity.Description
}
}
新手过度简化的经典案例!