Search code examples
c#parent

Send information to main class


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 ?


Solution

  • 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();
                }
            }
        }
    }