Questions tagged [using-directives]

The `using` directive, available in several languages including C# and C++, introduces members of a namespace into the current identifier search scope.

The C++ using directive allows the names in a namespace to be used without the namespace-name as an explicit qualifier. Of course, the complete, qualified name can still be used to improve readability.

// C++ Example:
using std::cout

The C# using directive is used to qualify namespaces and create namespace or type aliases. The scope of a using directive is limited to the file in which it appears.

// C# Example:
using System.Text;
using Project = PC.MyCompany.Project;

Creating an alias for a namespace or a type in C# is called a "using alias directive", and is illustrated in the code sample below:

namespace PC
{
    // Define an alias for the nested namespace. 
    using Project = PC.MyCompany.Project;
    class A
    {
        void M()
        {
            // Use the alias
            Project.MyClass mc = new Project.MyClass();
        }
    }
    namespace MyCompany
    {
        namespace Project
        {
            public class MyClass { }
        }
    }
}
289 questions
0
votes
1 answer

What's the relevance of the Note in [over.load]/1?

(You might see this question as a duplicate of this, but, to be honest, I've not really understood the matter, so I'm asking separately with my own wording.) [over.load]/1 reads: Not all function declarations can be overloaded. Those that cannot be…
0
votes
1 answer

Why does StyleCopAnalyzers think that global usings should be declared within a namespace, when such syntax is not possible?

I am creating a NUnit test project and in the creation of that, MS Visual Studio has created a file called Usings.cs with the line global using NUnit.Framework; which tells the project to include the NUnit framework in every file. I have been…
suitendaal
  • 149
  • 9
0
votes
0 answers

Type alias with "using" inside class cannot be used as return type

I'm currently switching from Java to C++ and I have troubling understaning the following problem. I have a header file for a class (AClass.h): #ifndef ACLASS_H #define ACLASS_H #include class AClass { public: using byte =…
E_3
  • 181
  • 7
0
votes
0 answers

How do I make my library work with using-directive?

I created a class library with .Net 6.0 which looks like this: namespace MyLib { public static class MyLib { public static void someFunction() { } } } I created a nuget package and added that package to another application. My…
TigersEye120
  • 664
  • 1
  • 9
  • 28
0
votes
2 answers

simplify using base template class statement

Often in derived template classes I need to refer to the base to access members. I end up writing code like this: template struct BaseClass { }; template struct Derived : public BaseClass { using Base =…
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
0
votes
2 answers

Get template parameter of derived class from base object

In short: I have a base class A_base without template parameters and a derived class A that has two. The function foo() only accepts base class objects foo() is supposed to return an object which has the same type as the derived classes' first…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

working of `line compiler directive in system verilog

Can someone please explain the working of `line compiler directive in system verilog tried to read it's working from LRM but was not able to understand it
0
votes
1 answer

Unqualified name lookup does not look in local namespace after using declaration

namespace A { int overloaded_f(float some_float); enum class Enum { Value }; } namespace B { int overloaded_f(A::Enum some_enum); int f(A::Enum some_enum){ using A::overloaded_f; // using B::overloaded_f; //…
0
votes
3 answers

using ifdef and ifndef directives to include header files

Please excuse my basic question and poor programming knowledge. I have an implementation that I need to use in many of my projects. But the included header files are different for different projects. Say I have spi.h header file to be used in…
SSR
  • 21
  • 3
0
votes
1 answer

How to apply mask to a default value in Typescript

I have a mask-phone directive that works perfectly in the input when the user write a value, but I need to set a default value and I don't know how to apply the mask directive in the component method. The value is show it in the input without the…
0
votes
0 answers

How to automatically find and add the namespace using clause of a type in Visual Studio?

When I try to write some function I have to add manually the required namespace. Is there a any keyword or shortcut for auto-add the using clause like ReSharper does? Thanks.
Kayhan
  • 69
  • 7
0
votes
1 answer

AngularJS custom directive for form validation for date inputs

I have an app written in Angular 1.5.3. Here is what I want to do: I have a user form with 2 date input types. I need to add some custom validation to my form. I want to show an error message to the user when the "expiry date" on the form is greater…
Aubrey Quinn
  • 311
  • 1
  • 3
  • 10
0
votes
2 answers

Encapsulate using directive statements C#

I have a number of using directives declaring the namespaces I am going to use in multiple classes. Is there a way to refer multiple using statements via a single using directive or any other approach to this solution that I can implement for all…
imujjwalanand
  • 309
  • 4
  • 15
0
votes
1 answer

Set using namespaces for all files in a folder

Is there a way to set the namespaces references for all files within a folder? Example: All my classes in a certain folder needs to reference System.ComponentModel (and many others) and I don't want to set the references every time I create a new…
0
votes
1 answer

The type name XXXX does not exist in the type XXXX

I have the following code (which compiles and works fine if I leave the warning in, I haven't tested it using the global:: hack): namespace NotifierService.Models {} namespace NotifierService { using Models; } The 'using Models' statement is…