.net Class "is not a member of" Class .. even though it is?
Looking over some older code, I've run into a strange namespace error.
Let's say I have two projects, HelperProject and WebProject. The full namespace of each - as given in application properties - is myEmployer.HelperProject and myEmployer.Web.WebProject.
The pages in the web project are full of statements that use classes from the helper project. There are no imports/using statements but there is a ref开发者_如何学Goerence to the helper project added in the bin. A few example lines might be:
myEmployer.HelperProject.StringHelper.GetFixedLengthText(Text, "", Me.Width, 11)
myEmploter.HelperProject.Utils.StringHelper.EstimatePixelLength(Text, 11)
However every line that is written in this manner is throwing the error 'HelperProject' is not a member of 'myEmployer'. If you declare the statements like this:
HelperProject.StringHelper.GetFixedLengthText(Text, "", Me.Width, 11)
HelperProject.Utils.StringHelper.EstimatePixelLength(Text, 11)
Everything seems fine.
In the solution object browser and the bin folder, HelperProject appears with its full namespace, myEmployer.HelperProject.
I don't want to have to change all the statements, and besides I suspect this is masking a more fundamental problem here. But I have no idea what's going on. Can anyone offer any pointers please?
Cheers, Matt
Have you got a namespace clash, is myEmployer a class or sub namespace of the namespace where the call is being made. Try changing on of the references to the following format:
global::myEmployer.HelperProject.StringHelper.GetFixedLengthText(Text, "", Me.Width, 11)
Edit:
If you add the below using statement it will should remove the ambiguity by telling the compiler that in the context of this class myEmploye. means global::myEmployer and not any other class or namespasce called myEmployer.
using myEmployer = global::myEmployer;
The namespace in application properties only applies to new files created. If you change the namespace in their without changing existing files, their namespace will be as specified in the files.
First, as ck said, the default namespace in a project's settings is only used to give a namespace when ceating new (class) files. So you should check that in your files you have the correct namespace. For example:
namespace myEmployer.HelperProject
{
public class StringHelper
{
public static string GetFixedLengthText(...)
{}
}
}
If this is different, ex. namespace HelperProject
, you would get the error.
If the namespaces are correct, you may have a local variable or field or similar also called myEmployer. Try right-clicking on myEmployer and choose "Go to definition" (or press F12).
P.S.: Standard casing for namespaces is PascalCasing, i.e. MyEmployer.*
精彩评论