bernie2024
✅ Strange error when trying to retrun a class type variable through a public method
Hi there, I am new to C# and while working on this method:
The method public InventoryItem GetNewItem(){} is displaying a build error which says "Inconsistent accessibility: return type 'inventoryitem' is less accessible than method 'GetNewItem()"
I dont understand what it means, the class must be public for later use. Am I doing this wrong?
8 replies
✅ 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
Save Method: will be posted as a comment so I dont run out of available words.
12 replies
❔ ✅ Struggling with function showing error, "Object reference not set to an instance of an object"
have been trying to solve this issue, not sure what the problem is. public static class CustomerDB
{
private const string dir = @"C:\C#\Files";
private const string path = dir + "Customers.txt";
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));
// write each customer
foreach (Customer customer in customers)
{
textOut.Write(customer.FirstName + "|");
textOut.Write(customer.LastName + "|");
textOut.WriteLine(customer.Email);
}
// write the end of the document
textOut.Close();
}
I get the error "Exception Thrown System.NullReferenceException: 'Object reference not set to an instance of an object. customers was null."
If any additional reference code is needed for context I can provide it, the point of this is to add a "Customer" to a database of customers
What am I doing wrong here? it only has the issue once reaching the 'Foreach (Customer customer in customers)' statement.
63 replies