we always add namespace at the top of the every form in win apps
like
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Business;
using System.Data.SqlClient;
using Business;
and most of namespace are common in most form but in asp.net there is a provision to add namespace only once in web.config file. so we dont have to add it in all web form and it save time. so i just need to know is there any same sort of provision for our win apps. how to achieve it. thanks
No, there isn't any sort of provision for your WinForm applications as there is for ASP.NET applications using the <namespaces> section in web.config. Also note that this applies only to .aspx pages and not code behind C#. In C# you have to add proper using statements in order to bring the types you want to use into scope.
One method is to add the standard using directives into the common templates.
You don't mention which version of visual studio you are using but in VS2010 you can find the templates in:
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
Change the Class.cs file in the zip to include the usings you want by default.
The only downside to this is that it is per machine - so you will need to do it on each developers computer - although I daresay you could roll it out with Active Directory (that's a question for serverfault.com).
I believe it also possible to set up team templates - but it's not something I have tried. More information available here:
http://msdn.microsoft.com/en-us/magazine/cc188697.aspx
Related
This is a scenario I identified when we turned on Stylecop rule SA1210 in our project.
The below is the set of using in one file I tried this sorted by visual studio's Remove and Sort usings.
I have configured to use System Directives first. Also, I have configured to automatically sort usings on file save.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Bss.Cloud.Api.Caches.Redis;
using BSS.Angel.BusinessEntities.Cache;
using BSS.Angel.BusinessEntities.Lists;
using BSS.Angel.BusinessEntities.Repositories;
using BSS.Common.DataAccess.Claims;
But the above raised Stylecop SA1210.
When I use VS's intellisence's auto fix, I get
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using BSS.Angel.BusinessEntities.Cache;
using BSS.Angel.BusinessEntities.Lists;
using BSS.Angel.BusinessEntities.Repositories;
using Bss.Cloud.Api.Caches.Redis;
using BSS.Common.DataAccess.Claims;
The reason being Cloud > Angel.
This makes it difficult for me as when I save, It gets sorted back to the old VS' Sort Using way.
Their reason being BSS > Bss according to Visual studio.
Now I cannot save the SA passing way(I have automatic sort enabled). VS does not support style cop way.
We cannot update the BSS to Bss as this is a huge change in a different library project for us.
Is there something I can do for Stylecop to be compatible with Visual Studio and not disable Automatic Sort using on save.
I'm used C# -some- but not recently.
I've got this header from some code, and it calls a custom non-system library, (Dynastream.Fit, at the bottom), but I need to know how to tell the program where it resides so it can compile. Pretty sure the library is in the SDK I downloaded, just need to find it. This is the FitSDK from ANT+.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Dynastream.Fit;
You need to add the dll of Dynastream.Fit to your project Reference folder by right clicking the reference folder -> add reference -> Browse -> choose the dll; unless you have already installed the assembly in GAC.
I'm writing a C# Console Application that is targeted towards .Net 4.5. I want to use Xaml Services to save and read a List data structure to file. I'm using VS 2013 Pro.
The .net doc's say Xaml has been in .NET since 4.0(?) I have my projected targeted to 4.5, but even with 4.0, 4.5.1, 4.6, and 4.6.1... same missing assembly reference. I'm doing a
using System;
using System.Collections.Generic;
using System.IO;
using System.Xaml; // <-- this is getting the assembly error
using System.Xml;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
But that is where the missing reference is getting the error...
I've used it before in Win Forms. It is that maybe it's excluded for console applications? Or am I missing something that Xaml Class depends on before using that assembly?
Could it be that you did not add a project reference to the assembly "System.Xaml.dll"? The Xaml functionality is not contained in the assemblies which are included in a new VS console project by default. (Right-Click on the "References" entry of the project, then select "Add References", then browse to "Framework" and look for System.Xaml, which refers to the dll of that name).
A namespace however does not necessarily correspond uniquely to an assembly, so you might require even more assemblies. If you know which types you need, you can browse the MSDN documentation for looking which assembly might still be required.
I am currently trying to cleanup a bit of a corporate website that I inherited here. I managed to clean up most of the errors in the website but something is still up here.
I have one masterpage that have this code :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage : System.Web.UI.MasterPage {
public lists m_listsClass = new lists();
(no it's not a typo the S in lists).
Now in App_code I have one class lists.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for lists
/// </summary>
public class lists
{
public lists()
{
When I try to build the website in visual studio 2008 I have this error :
Error 3 The type or namespace name 'lists' could not be found (are you missing a using directive or an assembly reference?) C:\Users\egirard\Documents\Visual Studio 2008\Projects\iFuzioncorp\iFuzioncorp\Masters\MasterPage.master.cs 23 12 iFuzioncorp
Am I missing something ?
Also I saw some strange behaviour on the server. Apparently according to IIS7 it is compiling using .net 2.0 (the pool is configured with .net 2) but there are some "using" statements that include Linq ... how is it possible to compile the page if Linq is not part of the .net 2 itself ?
Just edited with news details on the code itself. No namespace at all everywhere.
Hi There – i had a similar problem; all my namespaces and inheritance was in place. Then i then noticed that the class file’s build action was set to “Content” not “Compile” (in the properties window.
For whatever worth might there be for an answer (possibly not the right one) after many months, i think i should contribute this:
There is a case that this happens, when you place a web site inside another (ie in a subfolder).
In that case, the only App_Code folder that is legitimate is the App_code folder of the outer web site. That is, the App_Code folder right under the root of the master web site.
Maybe (say maybe) there should be no need to transform your web site to a web application, if you place the class file inside the App_code folder of the ROOT web site.
include the namespace under which lists calss is defined
or
define both the master page and lists class under the same namespace
Finally I understood quite lately that it was a website and not a web application I had to question the guys here to get it... So it's quite normal all the error I had. I haven't had the occasion to convert it first.
Make sure that in your masterpage, you have an #include statement for the namespace that the lists class is a part of (if they're in seperate namespaces, the masterpage isn't going to automatically pick up on it).
As for the strange server side behavior, .NET 2.0, 3.0, and 3.5 all run inside of .NET 2.0 app pools in IIS. It looks strange at first, but you get used to it. Here's a link with a little more in-depth explination:
The Way I See It: Where is ASP.NET 3.5 on IIS?
In order to use a type, you must reference the assembly which defines it and include the appropriate namespace.
Using only includes namespaces, if you don't use any types from said namespaces it has no effect.
is lists.cs wrapped in a namespace? if it is the case then you need to add the namespace (yournamespace.lists) or include it in masterpage. Check Also if your MasterPage is in a Namespace
Modify the Build Action of the App_Code Class to Compile.
Clean and Build the Project
This worked for me.
I had this problem myself
It wasn't working because the project I was referencing was set as a console application in its properties instead of a class library
Is there a way to reference a namespace globally across the whole solution?
So instead of having these lines in every code file:
using System;
using MyNamespace;
having to declare them only once, and every code file would use them.
Btw I am using Visual Studio.
No, C# doesn't have this concept. Each source file is independent in this respect. (And if the using directives are in a namespace declaration, those are independent from other using directives in peer namespace declarations, too. That's a pretty rare case though in my experience.)
You don't need ReSharper to change what gets included in a new class though. You can use the Visual Studio templates.
EDIT: Just to clarify the point about using directives within namespaces, suppose we had (all in one file):
using Foo;
namespace X
{
using Bar;
// Foo and Bar are searched for code in here, but not Baz
}
namespace Y
{
using Baz;
// Foo and Baz are searched for code in here, but not Bar
}
Usually I only have one namespace declaration in a file, and put all the using directives before it.
No, this is not possible.
If you're using ReSharper, you can set an option to include specific using directives in every new file you create though.
From this SO question and follow-up blog post. You can edit the Visual Studio default templates.
To do this, look at the file in this zip : [Program Files][Visual Studio]\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
and modify the Class.cs file as needed. Additionally, Visual Studio may have cached this file here :
[Program Files][Visual Studio]\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip
In C# 10.0 you can use Global Usings.
global using System;
global using MyNamespace;
No, you can not reference a namespace globally across the whole solution in .NET or .NET CORE.
But you can use project wise namespace globally in solution. this feature will be available from c#10/.NET 6. currently it's in preview but it will be released in NOV 2021
=========Project level .NET 6 global using namespace=========
Create a class file at root of the project e.g GlobalNamespace.cs
global using System;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
Then you don't need to declare using namespace in other .cs files of the project which are already declared globally.
As others have mentioned Visual Studio Templates are the way to go.
Note that simply adding a using statement to your template will not ensure that the compiler can resolve your types. So, if you are adding a using statement for MyNamespace in every class you may need to add an assembly reference to your project as well. See the C# FAQ for more information.
One trick I miss as a newb to CSharp is to look at the "refences" (in VS), to right click and "Add New Reference". This is especially handy when combining mulitple projects where I have made some generic class for reuse elsewhere.