首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

类型不符?该怎么解决

2012-03-03 
类型不符?public static void DisplayByPrice(Book[] arrBooks){intbookPrice arrBooks.GetUpperBound(0

类型不符?
public static void DisplayByPrice(Book[] arrBooks)
  {
  int bookPrice = arrBooks.GetUpperBound(0) - arrBooks.GetLowerBound(0) + 1;
  int [] lengths = new int [] { bookPrice };
  int[] lowerBounds = new int[] { 0 };
  Array arrPrices = Array.CreateInstance(Type.GetType("System.String"), lengths, lowerBounds);
  for (int i = arrBooks.GetLowerBound(0); i <= arrBooks.GetUpperBound(0); i++)
  arrPrices.SetValue(arrBooks[i].dblPrice,i);
  Array.Sort(arrPrices, arrBooks);
  foreach (Book item in arrBooks)
  {
  Console.WriteLine("{0} {1} {2}", item.dblPrice, item.strName, item.strAuthor);
  }

  }
系统提示arrPrices.SetValue(arrBooks[i].dblPrice,i);这里有错“未处理InvalidCastException”请问怎么改?


[解决办法]
Array arrPrices = Array.CreateInstance(Type.GetType("System.String"), lengths, lowerBounds); 
改成
Array arrPrices = Array.CreateInstance(Type.GetType("System.Double"), lengths, lowerBounds);
[解决办法]

探讨
Array arrPrices = Array.CreateInstance(Type.GetType("System.String"), lengths, lowerBounds);
改成
Array arrPrices = Array.CreateInstance(Type.GetType("System.Double"), lengths, lowerBounds);

热点排行