Inspire Dragon
Inspire Dragon

Reputation: 17

C# Error CS024 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?)

I am fairly new to C# and I run into this error despite the number of remedies I have tried. I am using a form..

The error states:

Error CS0246 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?)

My code looks like:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

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

My design code looks like this (Form1 is underlined with red):

namespace WindowsFormsApp1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Upvotes: 0

Views: 11176

Answers (2)

Talha
Talha

Reputation: 19282

In my case I had:

Referenced DLL : .NET 4.6

Project : .NET 4.5

Because of the above mismatch, the 4.5 project couldn't see inside the namespace of the 4.6 DLL. I recompiled the DLL to target .NET 4.6 and I was fine.

Upvotes: 0

abto
abto

Reputation: 1628

As Hans Passant mentioned your Form1 class is in the Timer2 namespace.

You can fix this by either

  • use the fully qualified namespace to instaniatate Form1

    Application.Run(new Timer2.Form1());
    
  • add an using statement for the Timer2 namspace to your Program class

    namespace WindowsFormsApp1
    {
        using Timer2;
    
        static class Program
        {
    
  • or change the namespace in your Form1 class

    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
    

    but then you have to make sure to do the same in the Form1.Designer.cs file also.

Upvotes: 1

Related Questions