简体   繁体   中英

Why am I getting this error even though I am simply intializing?

I keep getting this error for this block of code even though this is the beginning of the program and I am simply initializing. What do these errors mean?

Code:

public delegate void ProgressHandler(object myObject, CompareFilesUtilityEventArgs myArgs);
private ProfileClass _profile = new ProfileClass();
private FileClass _ComparisonFileOne = new FileClass();
private FileClass _ComparisonFileTwo = new FileClass();

Errors:

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

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

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

Suppose you have a Console program, of which the namespace is named FirstNamespace . You declare a ClassA inside this namespace. Then you have a second namespace, named SecondNamespace , and in there you have another class, named, ClassB .

Now, from inside FirstNamespace , if you create an object instance of ClassA , that will be fine, as both the ClassA and the calls in which the object is created ( Program class in the example below) are in the same namespace. But if you try to create an instance of ClassB , the compiler will complain as it is not 'aware' of the ClassB which is inside of a different namespace.

namespace FirstNamespace
{
    class Program
    {
        static void Main()
        {
            ClassA classA = new ClassA();
            ClassB classB = new ClassB();

            Console.ReadLine();
        }
    }

    public class ClassA
    {

    }
}

namespace SecondNamespace
{
    public class ClassB
    {

    }
}

Here, the line that instantiates classB gives me the following error:

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

To fix this, I must include a reference to the SecondNamespace using a using directive, just outside the namespace decleration:

using SecondNamespace;

namespace FirstNamespace
{
    ...

Now the FirstNamespace is aware of the SecondNamespace , and ClassB will be visible to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM