1

I am very new to c# and this is probably a very n00b error.

For this project I have been handed existing code to work with. The structure of the code is that it has a main solution with simulation as a supporting namespace.

I copied one of the classes (Adt_12) from simulation namespace that I want to modify and renamed it (Pb_cs2). The way I copied is, was to click on save as.. and then changed the file name to the new name I want. And then changed the public class name (and the constructors) to this new file name. I have rebuild 'simulation' and it rebuilts fine.

But when I try to call Pb_cs2, it is throwing the above 'the type or namespace named Pb_cs2 could not be found'.

The way I am using it in the executable class in main; is public static Pb_cs2 pb; (which was originally using Adt_12).

But it can still find Adt_12 in the solution and namespace. Just no Pb_cs2. I have rebuilt and built the solution.

The common error of .NET framework is not relevant.

Any ideas why this is happening and how I can fix this? I really dont want to modify the original file.

8
  • 4
    those class names could be, well, ... better! Commented Aug 28, 2012 at 7:51
  • Is this class shown in your solution explorer? It is the upper right window. Commented Aug 28, 2012 at 7:53
  • possible duplicate of the type or namespace name could not be found Commented Aug 28, 2012 at 7:54
  • You created a new file using 'Save As'. Have you included that new file in your solution? Commented Aug 28, 2012 at 7:56
  • The class names represent different machine part numbers. But better names would definitely help! Commented Aug 28, 2012 at 7:57

4 Answers 4

1

Take a look here. Visual Studio saying name doesn't exist in current context

You need to make sure:

  1. Your class name and namespace are not the same, like Pb_cs2.Pb_cs2 as this will confuse the compiler

  2. You can fully qualify the path to the class i.e. MyNamespace.MyNestedNameSpace.MyClass

  3. You can use a shortcut i.e. using MyClass = MyNamespace.MyNestedNamespace.Class1

  4. Ensure that your projects are targeting the same framework i.e. .NET 4.0 / .NET 4.0 Client Profile.

  5. You might have a collision where your class has the same name as another class, in which case, use option 2, or rename your class to something else.

  6. If your class name does not appear in intellisense, then it does not know where to look for it. You can right click the class and click "Resolve" which will give you some options on how to qualify your class.

...that is all I can think of right now!...Good Luck!

Edit:

Look up C# stylistic conventions... those class names are ugly!!!

Sign up to request clarification or add additional context in comments.

2 Comments

I cannot fully qualify the path to the class. The main class cannot find Pb_cs2 in the namespace. I did declare this class to be in the namespace (ie: namespace Simulation { Pb_cs2 { ... } }) and I am using the right (using Simulation) in the main class. Is there a step needed to add the new class to the solution? Rest of the points you mentioned checks out.
@GSam what access modifiers are being used on your class? i.e. public, abstract etc?
1

Add a reference to the namespace which contains the class you are calling. So you might have something like

namespace SomeNamespace
{
    public class Pb_cs2
    {
    ...
    }
}

so you need to add using SomeNamespace; to the declarations at the top of the file that is attempting to call your class. Or call the class using the fully qualified name

SomeNamespace.Pb_cs2 pbcs2 = new SomeNamespace.Pb_cs2();

You can also create a alias to the namespace when you reference it like

using NS = SomeNamespace;

then the above explicit reference can be called like

NS.Pb_cs2 pbcs2 = new NS.Pb_cs2();

I hope this helps.

2 Comments

Callig the class with the fully qualified name did not work. I reckon this file is not getting added as part of the namespace even though the structure is: namespace Simulation { public class Pb_cs2 { ... } } and in the executable I do have a : using Simulation.
Can you post the declarations you are using and the class declaration please? Also how you are instaciating the class object would help too... All the best.
1

Do it this way to be sure the calss is known by your solution. Project->addclass select class if it isn't selected by now. Name it and then add the new class. it should appear in your solution explorer. Now copy paste the code. rename the class the namespace should be fine.

and you should be okay with that.

Comments

0

Ran into this today. In my case, I had defined a class with a property of DateTime, then every usage of my new class was throwing an error ever time i went to set the property. the error...

the type or namespace of DateTime could not be found

and with that, I forgot to include at the top of my class...

using System; 

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.