My program has network functionality so to avoid messagebox spam in case of network problem i display errors from a collection.
internal class VM_Main
{ ...
public ObservableCollection<string> Errors { get; } = new observableCollection<string>();
...
}
This collection is displayed in my XAML window.
public MainWindow()
{ ...
vm_Main = new VM_Main();
dgdErrors.ItemsSource = vm_Main.Errors;
...
}
This is OK.
My problem is when i use objects from other classes in variables. How can I send information to the errors collection to report an error?
internal class VM_Main
{ ...
public ObservableCollection<string> Errors { get; } = new observableCollection<string>();
...
List <Customers> allCustomers = new List<Customer>()
}
public class Customer
{ ...
public string Name { get; set; }
...
public Customer()
{
try{ // Network work }
catch (Exception ex) { ????? Errors.Add(ex.Message) ???? }
}
...
}
"Errors doesn't exist in the current context." Of course, but how am I supposed to do ?
CodePudding user response:
Here is a fully OOP implementation design using singleton pattern (lazy approach). Further info Singleton pattern
1- Define the singleton class.
using System;
using System.Collections.ObjectModel;
namespace ErrorToConsole
{
public sealed class Singleton
{
private ObservableCollection<String> Errors = new ObservableCollection<string>();
private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance { get { return lazy.Value; } }
private Singleton()
{
}
public string AddError(string strError)
{
Errors.Add(strError);
return strError;
}
public ObservableCollection<string> GetErrors()
{
return Errors;
}
}
}
2- Calling the singleton class to add and retrieve errors.
using System;
namespace ErrorToConsole
{
class Program
{
static void Main(string[] args)
{
var singleton = Singleton.Instance;
try
{
//Adding some errors here
singleton.AddError("aaa");
singleton.AddError("bbb");
singleton.AddError("ccc");
singleton.AddError("ddd");
throw new Exception("Throwing exception");
}
catch (Exception ex)
{
//Adding exception message here
singleton.AddError(ex.Message);
//this is just to show that all the errors where added to the singleton collection.
foreach (var val in singleton.GetErrors())
{
Console.WriteLine(val);
}
Console.ReadLine();
}
}
}
}
CodePudding user response:
You make an interface:
public interface IErrorReport
{
void AddError(string message);
}
You then let your main window implement that interface:
internal class VM_Main : IErrorReport
{
...
public void AddError(string)
{
Errors.Add(string);
}
}
In your Customer constructor you add a parameter:
public Customer(IErrorReport report)
{
try
{
// Network work
}
catch (Exception ex)
{
Report.AddError(ex.Message);
}
}
Then all you need is to pass a reference to the main window when you create the customer:
internal class VM_Main
{ ...
public ObservableCollection<string> Errors { get; } = new observableCollection<string>();
...
List <Customers> allCustomers = new List<Customer>(this);
}