【问题标题】:Cannot implicitly convert type 'System.Collections.ArrayList' to 'BookReservationRQReservation[]无法将类型“System.Collections.ArrayList”隐式转换为“BookReservationRQReservation[]
【发布时间】:2018-03-01 19:36:20
【问题描述】:

我有一个 ArrayList 我想在其中添加项目然后用bookingrquest.Reservations 分配它我尝试了几次但我没有得到解决方案,如何将 ArrayList 转换为 BookReservationRQReservation[]

 BookReservationRQ bookingrquest = new BookReservationRQ();
  ArrayList reservation = new ArrayList();

  int count = 0;

  foreach (var item in reservation_details.selectedrooms)
  {
      reservation.Add(item.IDHotel);
      count++;
  }


  bookingrquest.Reservations = reservation;// i have this error in this line


   public BookReservationRQReservation[] Reservations {
     get {
        return this.reservationsField;
     }
     set {
         this.reservationsField = value;
    }
    }

【问题讨论】:

  • 好吧,看来您将bookingrquest.Reservations 定义为BookReservationRQReservationarray,这与包含@ 的arraylist 不同987654325@ 项目。你尝试创建一个BookReservationRQReservation 的数组怎么样?
  • IDHotel 是什么类型的?这是BookReservationRQReservation吗?
  • @RenéVogt IDHotel 正在搅动这不是他的问题,问题出在bookingrquest.Reservations = reservation;
  • 当然是(部分)问题。您不能将strings 放入BookReservationRQReservations 的数组中。如何从string 中创建BookReservationRQReservation
  • @RenéVogt 我用BookReservationRQReservation 更新了问题

标签: c# arraylist types


【解决方案1】:

既然你的请求对象需要一个数组,那么直接通过替换来使用数组怎么样:

ArrayList reservation = new ArrayList();

BookReservationRQReservation reservation = new BookReservationRQReservation[reservation_details.selectedrooms.Length];

假设 reservation_details.selected 是一个列表

然后替换

 reservation.Add(item.IDHotel);

reservation[iter++] = item.IDHotel;

在for循环之前,初始化iter=0

【讨论】:

  • 除非有从 stringBookReservationRQReservation 的隐式转换,否则不会编译。 OP 说 IDHotel 的类型是 string,而不是 BookReservationRQReservation
  • @RenéVogt,在回答时,该评论不可用。其次,在赞成或反对投票之前,我会尝试了解意图。如果是String类型,那么最后替换的语句可以相应增强
  • 我知道,这就是为什么我要求 OP 澄清而不是猜测这个问题的最重要信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多