Click here to Skip to main content
14,977,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi sir, i have one class in that i have 4 arrays in each array i have parameters so i tried to assign parameters in the array list using for-each loop but i got this error so please help me.

What I have tried:

This is my main class:
-----------------------
C#
public class BookRequest
{
        public string EndUserIp { get; set; }
        public string TokenId { get; set; }
        public string TraceId { get;set; }
        public int ResultIndex { get; set; }
        public string HotelCode { get; set; }
        public string HotelName { get; set; }
        public string GuestNationality { get; set; }
        public int NoOfRooms { get; set; }
        public string ClientReferenceNo { get; set; }
        public Boolean IsVoucherBooking { get; set; }
        public List<hotelroomsdetails> RoomInfo { get; set; }
        public int RoomIndex { get; set; }
        public string RatePlanCode { get; set; }
        public string RoomTypeCode { get; set; }
        public string RoomTypeName { get; set; }
        public List<bedtypes> bedtype { get; set; }
        public string SmokingPreference { get; set; }
        public List<string> Supplements { get; set; }
        public Price price { get; set; }
        public string CurrencyCode { get; set; }
        public decimal RoomPrice { get; set; }
        public decimal Tax { get; set; }
        public decimal ExtraGuestCharge { get; set; }
        public decimal ChildCharge { get; set; }
        public decimal OtherCharges { get; set; }
        public decimal Discount { get; set; }
        public decimal PublishedPrice { get; set; }
        public int PublishedPriceRoundedOff { get; set; }
        public decimal OfferedPrice { get; set; }
        public decimal AgentCommission { get; set; }
        public decimal AgentMarkUp { get; set; }
        public decimal TDS { get; set; }
        public decimal ServiceTax { get; set; }
        public List<hotelpassenger> hotelpass { get; set; }
}
Posted
Updated 11-May-21 9:14am
v2
Comments
Manish K. Agarwal 23-Jul-18 7:54am
   
please share the code where you assign the parameters using for each
ZurdoDev 23-Jul-18 7:57am
   
The error means you cannot loop over whatever objects you are trying to because they don't implement IEnumerator. But since you did not show us the code that actually creates the error I'm not sure what you want us to do.

You can't iterate over the properties of an object with a foreach or even a for loop - or you can, but it takes a good deal of work with reflection, and it creates code that is hard to maintain.
To use foreach the target object must e an instance of a class that implements iEnumerable or iEnumerable<T> - a Collection of other objects such as a List<T> or even an ArrayList. You can't just plonk an instance of a "random" class there and expect it to know that you want to perform the same task on each property.

If you want to assign parameters, you really should do it one by one, not try to work with a loop - you will get maintenance problems if you don't.
   
public class BookRequest : IEnumerable<KeyValuePair<string, object>>
{
public string EndUserIp { get; set; }
public string TokenId { get; set; }
public string TraceId { get;set; }
public int ResultIndex { get; set; }
public string HotelCode { get; set; }
public string HotelName { get; set; }
public string GuestNationality { get; set; }
public int NoOfRooms { get; set; }
public string ClientReferenceNo { get; set; }
public Boolean IsVoucherBooking { get; set; }

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToDictionary(p => p.Name, p => p.GetGetMethod().Invoke(this, null)).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}



now for iteration

var obj = new MyModel();
foreach(var kv in obj)
Console.WriteLine(kv.Key + ": " + kv.Value ?? "null");
   
Please using the typecast concept.
e.g:-
string[] arr = { "Anil", "Ali", "Akram", "Mahadev", "Lijo" };
TempData["var4"] = arr;

@{
foreach(var item in (string[])TempData["var4"])
{

@item


}
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900