-1

I have two classes and I have to do a foreach to insert the data of both shipping and billing address.


  //Class 1 
  public class shippingInformation
  {
   public string name {get; set;}

   public string address {get; set;}

   public string telephone {get; set;}
  }

 //Class 2
 public class billingInformation
  {
   public string name {get; set;}

   public string address {get; set;}

   public string telephone {get; set;}
  }

I have tried to put both the classes in the foreach statement, but it did not work.

foreach(var data in billingInformation && shippingInformation)
{
//Insert into the DB
}
Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • 4
    Well you can't iterate over *classes* anyway - you iterate over *collections*. (It would help if you followed .NET naming conventions.) Do you actually *have* any collections to iterate over? If not, what data did you actually expect to insert? Please provide more context. – Jon Skeet Oct 28 '22 at 11:43
  • do you have an array or list of `billingInformation` or `shippingInformation` objects? You need an array or list to use a `foreach` loop. You can't just iterate over the members in a class. – frankM_DN Oct 28 '22 at 11:43

2 Answers2

2

The operator && returns a boolean, and a foreach loop must be done on list or array or other collections. You can use 2 foreach loops instead:

billingInformation[] binfo = ...;
shippingInformation[] sinfo = ...;
foreach (var bi in binfo) {
    DoWhatever(bi);
}
foreach (var si in sinfo) {
    DoWhatever(si);
}
Eitan
  • 21
  • 3
2

Assuming you have two collections / lists / arrays with the same number of elements, you can do:

// Example arrays
var billingInformation = new[] { 1, 2, 4 };
var shippingInformation = new[] { 1, 2, 4 };

for (int i = 0; i < billingInformation.Length && i < shippingInformation.Length; i++)
{
    var billingItem = billingInformation[i];
    var shippingItem = shippingInformation[i];
}

However, you can't simply do foreach property in both classes. For that, you'd have to create a custom implementation and implement IEnumerable. In other words, you'd have something like this:

public class ShippingInformation : IEnumerable
{
    public string Name { get; set; }

    public string Address { get; set; }

    public string Telephone { get; set; }

    public IEnumerator GetEnumerator()
    {
        yield return Name;
        yield return Address;
        yield return Telephone;
    }
}

Then you can use a foreach for the object:

var shippingInformation = new ShippingInformation();
foreach (var data in shippingInformation)
{
    // do something with your property which is of type object?
}

If you are sure that your properties that you want to be usable in a foreach are always of the same type (e.g int), in that case you can implement IEnumerable<int> instead and yield return your integer values inside the GetEnumerator. For that I would suggest you to check out this question asked by another user: "How do I make a class iterable?".