3

I'm using C# .net 4.0 VS 2010.

I copied the following code here in Stackoverflow and confirmed all from the reference that this is suppose to work, but i am having syntax error on my call on the "Application.Run(new ShoutBox());" the error is "The type or namespace 'ShoutBox' could not be found."

The project was originally build as a console application. I just recently added a windows form named ShoutBox and is saved as ShoutBox.cs. I have transfer my code to the form so it does not display stuff in console rather on a textbox of the windows form i created.

What did i missed? And how can i make it work?

    using System;
    using System.Windows.Forms;

    namespace ChatApp
    {
        class ConsoleApplication1
        {


            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();


                //this one works
                Application.Run(new Form()); // or whatever


                //this one does not work, error on second ShoutBox
                Form ShoutBox = new Form();
                Application.Run(new ShoutBox()); 
            }


        }
    }

Just for reference, here is my final working code: This code creates a new Shoutbox form instead of a blanked form.

    using System;
    using System.Windows.Forms;
    using ShoutBox; // Adding this

    namespace ChatApp
    {
        class ConsoleApplication1
        {        
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Form ShoutBox1 = new ShoutBox.ShoutBox(); //Changing this
                Application.Run(ShoutBox1);               //Changing this
            }
        }
    }

where my Shoutbox form are as follows:

    using System
    using System.Windows.Forms;
    namespace ShoutBox
    {
        public partial class ShoutBox : Form
        {
    ....
1
  • This is a simple compilation error. Search for "The type or namespace could not be found." You're either missing a Reference (is the ShoutBox class in a different Project?) and/or missing a using (is the ShoutBox class in a different Namespace?) and/or it does not exist (is the ShoutBox class defined anywhere?) - there are many answers that cover this and it has nothing inherently to do with Forms. Quite simply: the code in question cannot resolve the ShoutBox type. Commented Jul 14, 2013 at 22:37

3 Answers 3

5

ShoutBox is the name of a variable referencing a Form, you can't call new ShoutBox().

You have already instantiated the form in the previous line, now you call simply

 Application.Run(ShoutBox); 

But, if you have a form called ShoutBox defined in this way

namespace ShoutBox
{
     public partial class ShoutBox: Form
     {
        .....
     }
}

then you need to add the using declaration at the beggining of your file

using ShoutBox;

or you can simply change the namespace used in the ShoutBox.cs file to the same namespace used in the program Main file

namespace ChatApp
{
     public partial class ShoutBox: Form
     {
        ....    
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This solves the problem. Thanks. I'll accept it as soon as I'm able.
0

You are missing either one or two things.

Firstly, you need to import the namespace that ShoutBox is in:

using Your.Namespace.Where.ShoutBox.Is.Declared;

An easy way to do this in Visual Studio is to put your cursor somewhere on the word ShoutBox and either press Alt+Shift+F10 or.. as some people who are much more productive than I am do, press Ctrl+.. This will bring up a menu that will show the namespace that needs to be included.

Also, if that namespace is in another assembly (you alluded to that), then you need to add it as a reference to the project.

Also, this:

Form ShoutBox = new Form();
Application.Run(new ShoutBox());

..is incorrect. I would suggest a tutorial on basic class creation.

Comments

0

The ShoutBox class is propably in different namespace.

And the code Form ShoutBox = new Form(); is useless, you need only Application.Run(new ShoutBox());.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.