0

I added following code to my C# windows form application to show a message box when i click the closing button.. But it gives me following error..

Error CS0116 A namespace cannot directly contain members such as fields or methods ebay source C:\Users\Supun\Documents\Visual Studio 2015\Projects\ebay source\ebay source\Form1.cs 107 Active

this is the code i used..

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult dialog = dialog = MessageBox.Show(
      "Do you really want to close the program?", 
      "SomeTitle", 
       MessageBoxButtons.YesNo);

    if (dialog == DialogResult.No)
    {
        e.Cancel = true;
    }
}

What do I need to do to fix it please?

JsAndDotNet
  • 16,260
  • 18
  • 100
  • 123
Ebay Boosting
  • 37
  • 1
  • 1
  • 4
  • 3
    Can you share your full class code with namespace, it seems like you are declaring some variables directly under namespace and not under class – Arun Kumar Sep 13 '18 at 09:52
  • 1
    "A namespace cannot directly contain members such as fields or methods"...it's a fairly clear message. You cannot put a field (property) or a method directly in a namespace. They must be inside a class. Check where you've placed the method and move it to the appropriate place (I imagine it belongs inside your form's class). – ADyson Sep 13 '18 at 09:56
  • 2
    PS. You know you could [google this exact error message](https://www.google.co.uk/search?q=A+namespace+cannot+directly+contain+members+such+as+fields+or+methods&oq=A+namespace+cannot+directly+contain+members+such+as+fields+or+methods&aqs=chrome..69i57j0l5.255j0j7&sourceid=chrome&ie=UTF-8) and get lots of previous hits telling you how to resolve it without having to ask the same kind of question again. – ADyson Sep 13 '18 at 09:57
  • 1
    I suggest you read https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0116 and https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/general-structure-of-a-csharp-program if you're unclear about how to structure a C# program. – ADyson Sep 13 '18 at 09:59

3 Answers3

12

I would imagine you've declared that function outside of a class?

Like

namespace Something
{
    private void Method()
    {
    }
}

Instead of

namespace Something
{
    class MyClass
    {
        private void Method()
        {
        }
    }
}
cgt_mky
  • 196
  • 1
  • 2
  • 7
2

Check your function is like below. I think some variable or function are directly under the namespace. Keep them inside the class.

namespace ConsoleApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
             DialogResult dialog = dialog = MessageBox.Show("Do you really want to close the program?", "SomeTitle", MessageBoxButtons.YesNo);
             if (dialog == DialogResult.No)
             {
                 e.Cancel = true;
             }
        }
    }
}
0

Don't write dialog twice, use the following simple code and you will be fine.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{

if (MessageBox.Show("Do you really want to exit??, "Exit", MessageBoxButtons.YesNo)== DialogResult.No)
 {
    e.Cancel = true;
 }
}
  • Welcome to `Stack Overflow`. The real cause of the error is as described in the accepted answer and some other responses and comments. Please read those responses and comments as it will help you understand the reasons, as well. I used to make the similar mistakes. – nam Jul 25 '19 at 15:10