I have a class called Message. It is located inside MyProgram namespace. The problem is that System.Windows.Forms has also a Message class, so whenever I want to use classes from both of the namespaces, I need to write MyProgram.Message to refer to my class, which is very tedious.
I hoped this line could work:
using static Message = MyProgram.Message;
but I get the following error:
a 'using static' directive cannot be used to declare an alias
Is there a way to force the code to recognize Message as MyProgram.Message? Also, changing my class's name is not a real solution. :P
using static is for static members, not nested types.
A regular using directive will work fine.
using Message = MyProgram.Message;
Related
I have a compiled class library with a root namespace Protege.MyLibrary.
It has a few root types, for example, CommonlyNamedType.
When I add the library to my consuming application, I'd like, for clarity in some situations, to be able to specify variables as:
using Protege;
...
MyLibrary.CommonlyNamedType oMyType = new MyLibrary.CommonlyNamedType;
rather than
using Protege.MyLibrary;
...
CommonlyNamedType oMyType = new CommonlyNamedType;
The former doesn't compile, indicating for the namespace Protege "Using directive is unnecessary", and that is can be removed.
This seems bizarre as I could go the other way and add additional namespaces, such as Protege.MyLibrary.AnotherNamespace.
I seem to be able to do this 100% okay in VB.NET - using either or both Imports Protege and/or Protege.MyLibrary and even optionally qualifying types with redundancy. But not in C#.NET.
I have had a good look around SO and other places and haven't seen an explanation for this behavior. Any ideas?
You can statically import a class like
using static System.IO.File;
It doesn't support for class, you need to use normal using for import the namespace.
I'm new and I don't know other way to explain so I posted my screenshot of project! Please help me to fix these errors... SCREENSHOT
You seem to be having a lot of problems with references and namespaces use.
First of all, you do not have a Card class defined. You only have a CardModel. Replace Card for CardModel and you will be good to go. Also, it seems you do not have a namespace declared on your class. Declare a namespace so you can use other classes in the same namespace (tipically the project name).
Second, if you are trying to use clases in another folder, you probably have to add the reference with the using keyword.
You're missing probably several using directives. Every class you write should be inside a 'namespace' You declare it after your using directives but before you start writing your classes, like this:
namespace WebShop.CardModel {
public class CardModel {
public string InsertCard(Card card){
And when you are working in the cardModel, unless Card is defined in the same namespace, you need:
using WebShop.Card;
Or whatever namespace you put Card in, that's what is throwing probably 99% of your errors, it is definitely the cause of all but one of the ones in the errors we can see in your screenshot.
I have the following two files:
IGlobalApiProvider.cs
using System.Collections.Generic;
using Vert.Slack;
namespace Vert.Interfaces
{
public interface IGlobalApiProvider
{
List<Im> ImList();
}
}
And the corresponding implementation: SlackApi.cs
using System.Collections.Generic;
using Vert.Interfaces;
namespace Vert.Slack
{
public class SlackApi : IGlobalApiProvider
{
public List<Im> ImList()
{
...
}
}
}
Now, Intellisense is telling me that when I use IM in IGlobalApiProvider it's resolving to Im, which is defined in a file named RtmStart.cs which has no namespace declaration. When I use IM in SlackApi.cs, it's resolving to Vert.Slack.Im which is defined in the Vert.Slack namespace in a file named Im.cs. The weird behavior alerted me to the redundant definition, so I removed it and things are working fine.
However, I'm confused about why Visual Studio behaved differently in these two ways. I can tell something was scanning for the class names in a different pattern in the two situations. I can also tell that being used in the same namespace vs being used in a class that uses the namespace seems to be the trigger. What I don't know is what mechanism controls the logic behind this behavior.
Can anyone shed light on this?
Everything you see is contained in Vert.dll, which consists of one project, Vert.csproj
Link to the four files mentioned in this post as they existed at the time of writing.
This has to do with the difference between the global and Vert.Slack namespaces.
The compiler looks for the most explicit namespace with the proper class defined.
In this example, when the compiler looks for the definition of Im in IGlobalInterfaceProvider.cs, there is no namespace defined (or used) in this file that contains the class, but Im is also defined in this file - which is declared in the global namespace.
When the compiler looks for the definition of Im in SlackApi.cs, Im is found in the explicit Vert.Slack namespace, and utilizes that class.
The answer here is a similar topic and may provide more insight.
This may be related to the fact that your namespaces are in the wrong place ;-)
http://www.stylecop.com/docs/SA1200.html
This answer here gives a good explanation: Should 'using' statements be inside or outside the namespace?
In XAML, a custom namespace can be imported with an xmlns directive:
xmlns:custom="clr-namespace:GreatStuff"
In C#, that namespace can be imported with
using GreatStuff;
Moreover, evaluation of that namespace based on the global (root) namespace can be enforced as follows:
using global::GreatStuff;
How can I enforce evaluation based on the global namespace in XAML?
Background:
I am facing the (admittedly slightly obscure) situation that there is such a namespace global::GreatStuff, which contains a WPF control class named ... GreatStuff (i.e. fully qualified, that's global::GreatStuff.GreatStuff in C#). In the same namespace, I want to use that control in a WPF window.
Interestingly, in this constellation, I cannot use the Name/x:Name attribute on any controls of type global::GreatStuff.GreatStuff in my XAML file for the window:
The type name 'GreatStuff' does not exist in type 'GreatStuff.GreatStuff'. (CS0426)
Note that the very same file compiles fine if I do not specify a Name/x:Name attribute!
Now, why should the compiler assume that by setting a Name/x:Name attribute, I am trying to access something called GreatStuff.GreatStuff.GreatStuff?
The answer can be found by examining the .g.cs file generated from the window XAML file. In that file, the XAML snippet
<custom:GreatStuff x:Name="stuff"/>
gets compiled to the following C# snippet:
internal GreatStuff.GreatStuff stuff;
That is, the fully-qualified name is used, but without the explicit global namespace marker.
Of course, as a field in a class in namespace global::GreatStuff, all of this is wrapped in
namespace GreatStuff {
And so, the poor C# compiler cannot help but assume that stuff is supposed to be of a type global::GreatStuff.GreatStuff.GreatStuff. This could be avoided if in
xmlns:custom="clr-namespace:GreatStuff"
I could enforce that any mentions of that namespace prefix could be converted while enforcing the global namespace.
For reasons external to this question, changing the namespace and/or class names is not an option here.
This issue only occurs when all of the following are true:
You have a class with an identical name as the namespace containing it.
Within that same namespace, you have a xaml file with an instance of an object that is also declared within that namespace.
That specific object instance is given a Name or x:Name attribute.
If you cannot modify anything to make any one of those conditions false, then you are working with some very serious restrictions.
As far as I know, this is a limitation of the xaml compiler, and you will need to contact Microsoft about resolving it. The only other thing I can think of would be to try to work around it with a custom build process that builds xaml, allows you to edit the generated files, then builds the code. That would require some research to figure out how to set up.
I have defined a user control, ExpressionControl, under the namespace:
TriggerEditor.UserControls
I have a form, "IfEditor", under the namespace:
TriggerEditor.Forms
When I add an instance of my control to the form (naming it expCondition), the following code is generated in the Designer to create the control:
this.expCondition = new TriggerEditor.UserControls.ExpressionControl();
This results in the following compilation error:
The type name 'UserControls' does not exist in the type 'TriggerEditor.Forms.TriggerEditor'
I don't understand why it is looking in "TriggerEditor.Forms.TriggerEditor"; that's not what the code says. Now, I can resolve this error manually by modifying the line, removing the "TriggerEditor." that preceeds "TriggerControls", as such:
this.expCondition = new UserControls.ExpressionControl();
This satisfies the compiler, however I obviously don't want to do this every time I add an instance of my ExpressionControl to a form. How do I avoid this situation?
It looks like you might have a TriggerEditor class within the TriggerEditor.Forms namespace; is this the case?
If so, because the TriggerEditor class is within your current namespace, you are "closer" to this class, and therefore it is looking for a subclass.
Avoid using the same name for a class and a namespace!?
Apparently, in the namespace TriggerEditor.Forms, you have a class named TriggerEditor in the same namespace as your IfEditor class.
So, when looking for TriggerEditor.UserControls.ExpressionControl, the compiler looks in the TriggerEditor class (which is in the same namespace, so closer) instead of the TriggerEditor namespace...
Normally, to avoid that, you would use a namespace alias qualifier, but being in a Designer generated class, you can't really control that.