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
int i = 0;
foreach(var result in resultSet)
{
ConflictingBooking found = new ConflictingBooking();
found.BookingNumber = conflictingBookingNumber;
found.BookingStarts = conflictingBookingDT;
found.Name = "found["+i++ +"]";
conflictingBookings.Add(found);
}
If that's not what you want please clarify your question
Variable names only exist as a matter of convenience to the programmer, and don't really exist at runtime. You cannot produce actual variables at runtime.
Because you're populating a list, however, you should have all the data required to produce a "name" for display purposes based on the index. For example:
for(int i = 0; i < conflictingBookings.Length; i++)
{
Console.WriteLine("found[" + i + "]: " + found.BookingNumber);
}
Simple solution: Add the loop counter to your constructor:
ConflictingBooking found = new ConflictingBooking(i);
Then add a method (or override ToString()
) to return the string concatenated instance name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.