1

I have a class like so:

public class OverDueClass
{
     public int CustomerID { get; set; }
     public int Question_ID { get; set; }
     public string Department { get; set; }
     public DateTime DueDate { get; set; }
}

And I am populating this class and passing it to another method:

while (dataReader.Read())
{
      OverDueClass overDueItem = new OverDueClass();

      overDueItem.CustomerID = (int)dataReader[0];
      overDueItem.Question_ID = (int)dataReader[1];
      overDueItem.Department = dataReader[2].ToString();
      overDueItem.DueDate = (DateTime)dataReader[3];

      OverDueCell.Add(overDueItem);
}  

sendEmail("[email protected]", OverDueCell);

Now in the other method, I can see the data is being passed.

Now I am trying to run a foreach to do something with the data and I tried the following:

foreach(string item in overDue)
{

}

But I get this error:

Cannot covert type to string.

2
  • 1
    overDue I'm assuming is a single OverDueClass, its not an array or collection, so you can't foreach over it. Are you trying to iterate through all the properties in the class? Commented Dec 2, 2015 at 21:25
  • @RonBeyer...Because he sent OverDueCell as a parameter to sendEmail method, I think overDue would be OverDueCell. Commented Dec 2, 2015 at 21:32

2 Answers 2

6

If the overDue is the OverDueCell then change string to OverDueClass in foreach like this:

foreach(OverDueClass item in overDue)
{

}

Or use var keyword:

foreach(var item in overDue)
{

}
Sign up to request clarification or add additional context in comments.

Comments

4

If you are attempting to iterate thru the collection of OverDueItems:

foreach(var item in overDueItemsCollection)
{
    ...
}

If you are trying to iterate thru properties of OverDueItem:

PropertyInfo[] properties = typeof(overDueItem).GetProperties();
foreach (PropertyInfo property in properties)
{
    var value = property.GetValue(overDueItem);
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.