2

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=netframework-4.8

Based on this documentation the code below should generate a message box, but all it does is print to the output tab:

How can I get it to produce the message box as advertised in the Microsoft docs?

//*********************************************************

//****Assignment 6 Section 1

//*********************************************************

Console.WriteLine("\n Assignment 6 - Asserts and Try/Catch.");

string fooString = null;
int fooInt = 0;

Debug.Assert(!String.IsNullOrEmpty(fooString), "Parameter must not be empty or null.");
Debug.Assert(fooInt > 0, "Parameter must be greater than zero.");

Here is the code from Form1.cs (tried with and without the Trace.Listeners code)

namespace Unit_6_WinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Trace.Listeners.Clear();
            DefaultTraceListener defaultTraceListener = new DefaultTraceListener();
            defaultTraceListener.AssertUiEnabled = true;
            Trace.Listeners.Add(defaultTraceListener);

            //*********************************************************

            //****Assignment 6 Section 1

            //*********************************************************

            Console.WriteLine("Assignment 6 - Asserts and Try/Catch. \n");

            string fooString = null;
            int fooInt = 0;


            Debug.Assert(!String.IsNullOrEmpty(fooString), "Parameter must not be empty or null.");
            Debug.Assert(fooInt > 0, "Parameter must be greater than zero.");

            //*********************************************************

            //****Assignment 6 Section 2

            //*********************************************************

            string[] fooStringArray = new string[5];

            try
            {
                for (int index = 0; index <= fooStringArray.Length; index++)
                {
                    var foo = fooStringArray[index];
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Array out of bounds error occurred.");
                Console.WriteLine("{0} \n", ex.Message);
            }

            //*********************************************************

            //****Assignment 6 Section 3

            //*********************************************************

            try
            {
                using (FileStream fs = File.Open("NoFileNamedThis.txt", FileMode.Open))
                {

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("File not found error.");
                Console.WriteLine("{0} \n", ex.Message);
            }

            //*********************************************************

            //****Assignment 6 Section 4

            //*********************************************************

            try
            {
                DivideByZero(1, 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("DivideByZero error occurred.");
                Console.WriteLine("{0}", ex.Message);
            }


            void DivideByZero(int dividend, int divisor)
            {
                if (divisor == 0)
                {
                    throw new ArgumentException("Divide by Zero");
                }
            }
        }

    }
}
747Aviator
  • 21
  • 2
  • 1
    This only works with apps that have a UI, since you are using a console app, this will just print to the output tab as you've found. – Ron Beyer Dec 30 '19 at 14:54
  • I was under the impression based on other articles showing MessageBox being used in a Console app, that it was possible. However, changing the project to Windows Application via project properties still doesn't generate the advertised message box. What did I miss? – 747Aviator Dec 30 '19 at 15:34
  • 1
    The message box will only be produced in a Windows application that has an [active message pump](https://stackoverflow.com/questions/2222365/what-is-a-message-pump). Simply changing the project type from Console to Windows app isn't sufficient as that doesn't automatically start a message pump. Create a new Winforms or WPF application, then try again. See the docs on [Application.Run](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.run?view=netframework-4.8) –  Dec 30 '19 at 15:36
  • Note that the documentation to which you refer is for .NET Framework 4.8, **not** .NET Core. The .NET Core-specific documentation is at https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=netcore-3.1 – Heretic Monkey Dec 30 '19 at 16:15
  • Your `Form` constructor doesn't run on a UI-thread (the default Program.cs code instantiates the form, *then* starts the message pump), so the Debug.Assert won't display anything. –  Dec 30 '19 at 16:38

1 Answers1

0

As per Microsoft docs here:

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=netframework-4.8

When the application runs in user interface mode, it displays a message box that shows the call stack with file and line numbers. The message box contains three buttons: Abort, Retry, and Ignore. Clicking the Abort button terminates the application. Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. Clicking Ignore continues with the next instruction in the code.

Since you are not running in UI mode, message is being shown on Console.

Again as per docs, to enable UI mode:

<configuration>  
  <system.diagnostics>  
    <assert assertuienabled="true"    logfilename="c:\\myFile.log" />  
   </system.diagnostics>  
 </configuration>  
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
  • How do you select "user interface mode"? – 747Aviator Dec 30 '19 at 16:00
  • Pls see my updated answer. I havent tried it. Just referenced through the docs. Try and let know – Gauravsa Dec 30 '19 at 16:04
  • @747Aviator As we noted in the comments, you need a Windows application with an active message pump. –  Dec 30 '19 at 16:04
  • I tried doing that with the console app with no luck. Maybe I didn't set up the config file correctly though. @Amy I tried to do this with a Windows Form Application and the lines Debug.Assert require the "Continue" button in the debugger rather than generating a popup message box. – 747Aviator Dec 30 '19 at 16:08
  • @747Aviator Show us please. Where in your Winforms application did you add the assert? Did you add it to the form code or to Program.cs? Please *show us*. –  Dec 30 '19 at 16:09
  • I think debug.assert works in debug mode lol. Is your app in debug mode – Gauravsa Dec 30 '19 at 16:16
  • @Gauravsa it is indeed, in debug mode. – 747Aviator Dec 30 '19 at 16:28
  • @Amy I added the code to my original post. It is in Form1.cs. I tried also to do it in Program.cs. No joy. Thanks for the help! – 747Aviator Dec 30 '19 at 16:35