- I have created a class (ConflictingBooking) that contains fields for various information in regards to a specific reservation.
- I loop through a database and select certain reservations based on certain criteria.
- I want to create an instance of the class with that selected certain criteria.
- I then want to add each of those class instances to a List which I will use elsewhere.
Problem I have is at step 3. I want to name the new instance something like “found[i]” where i is incremented for each reservation found:
ConflictingBooking found = new ConflictingBooking();
found.BookingNumber = conflictingBookingNumber;
found.BookingStarts = conflictingBookingDT;
conflictingBookings.Add(found);
In the above code, I need to replace ‘found’ programatically.
Whilst I would appreciate a simple code snippet to follow, I would also like to read some reference so I know what I’m doing :-). I just don’t know how to google my problem.
Seems I have not been entirely clear – here is the appropriate code:
dbManager.Open();
dbManager.ExecuteReader(CommandType.Text, string.Format("SELECT BookingDate, {0} FROM BTable WHERE Court IN {1} AND BookingDate IN ({2})", columns, facilityIDs, bookingDates));
using (IDataReader rdr = dbManager.DataReader)
{
decimal conflictingBookingNumber = 0;
DateTime conflictingBookingDT;
object result = null;
while (rdr.Read())
{
for (int i = 0; i < requestedColumnNames.Count; i++)
{
result = dbManager.DataReader[requestedColumnNames[i].ToString()];
if (result != null)
conflictingBookingNumber = Convert.ToDecimal(result);
result = dbManager.DataReader["BookingDate"];
if (result != null)
conflictingBookingDT = Convert.ToDateTime(result);
if (conflictingBookingNumber > 0)
{
int next = conflictingBookings.Count + 1;
ConflictingBooking found = new ConflictingBooking();
found.BookingNumber = conflictingBookingNumber;
found.BookingStarts = conflictingBookingDT;
// conflictingBookings is a List<conflictingBooking>
conflictingBookings.Add(found);
}
}
}
}
Hope that clarifies my problem.
I think all you are looking for is to put your code in a for loop. I might be completely misunderstanding you but it seems as if you want to do something like this
If that’s not what you want please clarify your question