C#: MessageBox.Show Error - method but used like a type
So I have the declara开发者_JAVA百科tion at the beginning of my class file
using System.Windows.Forms;
But when I try to issue the statement
MessageBox.Show("Pow");
I receive the error
Error 2 'System.Windows.Forms.MessageBox.Show(System.Windows.Forms.IWin32Window, string)' is a 'method' but is used like a 'type'
Complete code:
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Contacts
{
class AnotherClass
{
MessageBox.Show("Pow");
}
}
I can't seem to find any reason for this. The other oddity is that when I type MessageBox, Intellisense finds it, but after placing my ( . ) I don't receive the typical menu of method options from that class such as "Show".
Any thoughts?
It's simple really. Main() is the first function which executes in a C++ or C# application. and in your original code you have declared the namespaces, created a class but did not create a function. just used the MessageBox.Show method. In second example, you had created a function which you then put in the method MessageBox.Show and because the function is called Main() it is the first function to execute at the start of the program.
I figured it out nevermind. The principal of Class which can contain methods, properties and field declarations.
Got it. Should have renamed Main() to NotMain() in Program.cs then added code to AnotherClass. The code should look like this.
class AnotherClass
{
public static void Main()
{
MessageBox.Show("Pow!);
}
}
Not exactly why but it works... newbie
精彩评论