示例#1
0
        public string _name;  // if you have a property that does stuff, you can't use the auto implement option,
                              // so you have to make a field to hold the value outside of the property. convention is for "_" at the beginning of private fields.

        public void ExitNow() // 2. This is what happens when the event method is called
        {
            ExitingEventArgs args = new ExitingEventArgs();

            args.listName     = _name;
            args.listContents = shoppingList;
            ExitingProgram(this, args);
        }
示例#2
0
        static void OnExit(object sender, ExitingEventArgs args) // here's where the args are packaged. not su
        {
            ShoppingList shopList = new ShoppingList();

            bool success = false;
            int  tries   = 3;

            while (!success && tries > 0)
            {
                try
                {
                    using (StreamWriter outputFile = File.CreateText(@"c:\Created_Lists\" + args.listName + "-List.txt"))
                    {
                        shopList.PrintList(outputFile, args.listContents); // Some troube calling as the list itself was hard to access
                    }

                    using (StreamWriter outputFile = File.CreateText(@"c:\Created_Lists\" + args.listName + "-List.csv"))
                    {
                        shopList.CSVList(outputFile, args.listContents);
                    }
                    success = true;
                }
                catch (DirectoryNotFoundException)
                {
                    Console.WriteLine($"The file wasn't saved. There may be illegal characters in your list name \"{args.listName}\". Try another (y/n)?");
                    string userResponse = Console.ReadLine().ToLower();
                    if (userResponse == "y" || userResponse == "yes")
                    {
                        Console.WriteLine("Enter the new name.");
                        args.listName = Console.ReadLine();
                    }
                    tries--;
                    if (tries <= 0)
                    {
                        Console.WriteLine("This didn't work. Sorry, closing without saving.");
                    }
                }
                catch (ArgumentException)
                {
                    Console.WriteLine($"The file wasn't saved. There were illegal characters in your list name \"{args.listName}\". Try another (y/n)?");
                    string userResponse = Console.ReadLine().ToLower();
                    if (userResponse == "y" || userResponse == "yes")
                    {
                        Console.WriteLine("Enter the new name.");
                        args.listName = Console.ReadLine();
                    }
                    tries--;
                    if (tries <= 0)
                    {
                        Console.WriteLine("This didn't work. Sorry, closing without saving.");
                    }
                }
                catch (NotSupportedException)
                {
                    Console.WriteLine($"The file wasn't saved. There may be illegal characters in your list name, probably at the beginning. \"{args.listName}\". Try another (y/n)?");
                    string userResponse = Console.ReadLine().ToLower();
                    if (userResponse == "y" || userResponse == "yes")
                    {
                        Console.WriteLine("Enter the new name.");
                        args.listName = Console.ReadLine();
                    }
                    tries--;
                    if (tries <= 0)
                    {
                        Console.WriteLine("This didn't work. Sorry, closing without saving.");
                    }
                }
            }
        }