C
C#2y ago
bernie2024

✅ Cant convert a list to list of a class type?

I am new to C# and am stuck on this strange issue, I cant seem to send a list to a different class, as its only accepting a list of type "Customer" (class name), how would I convert or send this information over? Method which collects info and sends it to save method
public partial class frmCustomers : Form
{
List<Customer> customers = new List<Customer>();

private void btnAdd_Click(object sender, EventArgs e)
{
frmAddCustomer addCustomerForm = new frmAddCustomer();
Customer customer = addCustomerForm.GetNewCustomer();
if (customer != null)
{
CustomerDB.SaveCustomers(customer); /// This is the line giving me an error
MessageBox.Show(customer.GetDisplayText().ToString(), "BTNAddClick2");
CustomerListBox();

}
}
public partial class frmCustomers : Form
{
List<Customer> customers = new List<Customer>();

private void btnAdd_Click(object sender, EventArgs e)
{
frmAddCustomer addCustomerForm = new frmAddCustomer();
Customer customer = addCustomerForm.GetNewCustomer();
if (customer != null)
{
CustomerDB.SaveCustomers(customer); /// This is the line giving me an error
MessageBox.Show(customer.GetDisplayText().ToString(), "BTNAddClick2");
CustomerListBox();

}
}
Save Method: will be posted as a comment so I dont run out of available words.
3 Replies
bernie2024
bernie20242y ago
public static void SaveCustomers(List<Customer> customers)
{

// create the output stream for a text file that exists
StreamWriter textOut =
new StreamWriter(
new FileStream(path, FileMode.Create, FileAccess.Write));
foreach (Customer customer in customers)
{
MessageBox.Show(customer.FirstName + customer.LastName + customer.Email, "SaveCustomers1");
}

// write each customer
foreach (Customer customer in customers)
{
if (customer == null)
{
MessageBox.Show("2, 3");
throw new ArgumentNullException(nameof(customer), "Customer object cannot be null");
}

MessageBox.Show("2, 4");
textOut.Write(customer.FirstName + "|");
textOut.Write(customer.LastName + "|");
textOut.WriteLine(customer.Email);
MessageBox.Show(customer.FirstName + customer.LastName + customer.Email, "SaveCustomers1");
}

// write the end of the document
textOut.Close();
}
public static void SaveCustomers(List<Customer> customers)
{

// create the output stream for a text file that exists
StreamWriter textOut =
new StreamWriter(
new FileStream(path, FileMode.Create, FileAccess.Write));
foreach (Customer customer in customers)
{
MessageBox.Show(customer.FirstName + customer.LastName + customer.Email, "SaveCustomers1");
}

// write each customer
foreach (Customer customer in customers)
{
if (customer == null)
{
MessageBox.Show("2, 3");
throw new ArgumentNullException(nameof(customer), "Customer object cannot be null");
}

MessageBox.Show("2, 4");
textOut.Write(customer.FirstName + "|");
textOut.Write(customer.LastName + "|");
textOut.WriteLine(customer.Email);
MessageBox.Show(customer.FirstName + customer.LastName + customer.Email, "SaveCustomers1");
}

// write the end of the document
textOut.Close();
}
Error reads: "Cannot convert from type 'CustomerMaintenence.Customer' to 'Systems.Collections.Generic.List<CustomerMaintence.Customer>' Ignore the messageboxes, they are debugging code as I am 4 hours deep into this and no closer to getting it running.
jcotton42
jcotton422y ago
you're trying to convert from a single thing to a list of things you either meant to pass a List, or you need to wrap what you're passing in a List
bernie2024
bernie20242y ago
I DID IT sorry man I didnt mean to waste your time so I found the issue was I needed to do a customers.Add(customer); which is adding it to a list of type Customer now it can send over which would be the passing to to a list which you were talking about ok thank you very much sorry again wow bad timing on my part