Another question, is that different the using direction inside or beside
the namespace ?
You really need to give us more information than this, especially the exact
errors you get. If the project can't pass the compiler check there should be
compiler errors/warnings.
The 'using' keyword serves two purposes.
using System.IO;
namespace MyNamespace
{
// You can here type File instead of the full System.IO.File
}
Inside a method it serves the IDisposable interface and will automatically
call Dispose
void MyMethod()
{
using(StreamWriter sw = new StreamWriter("C:\\MyFile.txt"))
{
sw.WriteLine("Hello World");
}
}
when the using block ends it will call sw.Dispose(), which in turn will
Flush and Close the StreamWriter as well.
--
Happy Coding!
Morten Wennevik [C# MVP]
> I have a csharp project develop with vs2008, it can pass build
> first, when I add a new class to it, the other class in the project
> can't pass the compiler check, I can't check the new class now, It seem
> that all go wrong. anyone meet the trouble?
As Morten said, without more information, there's no way for us to
diagnose your problem. Obviously, the normal situation is that having
added a new .cs file to a project, it should still compile fine.
Of course, once you start adding things to the file, such as using
directives and actual code, there are lots of ways to make it fail. But
these aren't any different just because you've added a file to the
project, so I assume you aren't dealing with something simple like that.
> Another question, is that different the using direction inside or
> beside the namespace ?
Yes, there is a subtle difference between a "using" directive placed at
the top of the compilation unit (i.e., the .cs file) and a "using"
directive placed just inside a namespace declaration. The placement of
the "using" directive determines the scope of the "using" directive.
For compilation units that have only a single namespace declaration, the
two methods are for most intents and purposes the same. But the scoping
difference can show up in a compilation unit where multiple namespaces are
declared.
Pete
namespace DotNetNuke.Services.Vendors
{
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Data;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
public class BannerController
{
public void AddBanner(BannerInfo objBannerInfo)
{
DataProvider.Instance().AddBanner(objBannerInfo.BannerName,
objBannerInfo.VendorId, objBannerInfo.ImageFile, objBannerInfo.URL,
objBannerInfo.Impressions, objBannerInfo.CPM, objBannerInfo.StartDate,
objBannerInfo.EndDate, objBannerInfo.CreatedByUser,
objBannerInfo.BannerTypeId, objBannerInfo.Description,
objBannerInfo.GroupName, objBannerInfo.Criteria, objBannerInfo.Width,
objBannerInfo.Height);
}
.....
}
}
Now, i was told that error CS0234: “Utilities” is not exists in namespace
“DotNetNuke.DotNetNuke.Common”(miss some reference?) ,
In my opinion, no matter which file I add to this project, the error is
misleading, and should be a bug, or it's my fault?
The project's default namespace is DotNetNuke, and I think it's nothing
about the using directive , isn't it?
"mathphoenix" <mathp...@live.com> 写入消息
news:21AA3F68-D0C8-4C13...@microsoft.com...
In your code, if you have at least one class with the namespace
DotNetNuke.Common.Utilities in a referenced project the code should compile.
There may be an error if you at the same time have a Utilities class in
DotNetNuke.Common but the error message should in that case indicate that
Utilities already exists.
I assume you do have a class with this namespace, so there may well be a
bug. Try removing the using directive referring to Utilities and instead use
the full class name for BannerInfo and/or DataProvider. If this works, try
deleting the namespace part from BannerInfo/DataProvider and see if Visual
Studio is able to figure out where these classes are defineed (ALT+F10 when
the cursor is on the class name).
As for using directives inside or outside the namespace. I don't think it
matters at all as long as they are outside the class scope.
If you are able to reproduce the error in a new solution you can submit a
bug report at the Microsoft Developer Division Feedback Center
https://connect.microsoft.com/VisualStudio
(You may need to sign in to be able to submit a bug)
If you rather want me to submit the bug, send me a project where this bug is
reproducable and I'll be happy to submit a bug report.
--
Happy Coding!
Morten Wennevik [C# MVP]
One other thing that comes to mind... Are you by any chance adding a file
called utilities.cs to the project? Or some other named class that corresponds
either with a namespace or a class elsewhere in your project?
Jesse
--
Jesse Houwing
jesse.houwing at sogeti.nl
If you mean the difference between:
using System;
namespace Foo {
...
}
and
namespace Foo {
using System;
...
}
then yes, there is a subtle difference there for the single-namespace-
in-a-file case, when it comes to dealing with classes in the global
namespace. I'll just illustrate with code:
// Outside of any namespace
class Foo { ... }
namespace Bar { class Foo { ... } }
using Bar;
namespace Test1 {
using F = Foo; // Foo here is global::Foo
}
namespace Test2 {
using Bar;
using F = Foo; // Foo here is Bar.Foo
}
Basically, the way it works is this: it looks for the identifier in
scopes starting from the innermost one and going outside; and, for
each scope, it _first_ looks for identifiers declared in that scope
(i.e. in the same namespace, for a namespace), and only then looks in
namespaces referenced by "using" directives within that scope.
In practice, since common coding style is to avoid out-of-namespace
declarations, this difference is hardly ever observed.
For the case of multiple namespaces in a file, the difference is more
pronounced, but should be fairly obvious.
"Morten Wennevik [C# MVP]" <MortenW...@hotmail.com> 写入消息
news:0A88D47E-48D9-439A...@microsoft.com...
using System;
using System.Security;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using System.IO;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Portals;
using System.Collections;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Common;
using System.Threading;
using DotNetNuke.Services.Scheduling;
using VB2CS;
using System.Web.Hosting;
namespace DotNetNuke.DotNetNuke.Common
{
public class Initialize
{
........
}
}
I forgot to remove the default namespace. it's the fundamental reason?
Although I can't understand why the ide was confuse by this simple mistake,
maybe is a big one :)?
it's possible to reproduce the error by a simple project, but not which I am
working for, it's a huge solution and it's not suitable to illustrate the
problem.
I don't have extern time, may be you can send this bug(maybe it's not a bug)
to Microsoft connector.
even the ide told me the class is not exist, I'm sure she know it, because
the class is highlight:)
I am sorry to waste you time just because my careless mistake.
thanks for all replies
--
Code Your Life
mathphoenix
"Morten Wennevik [C# MVP]" <MortenW...@hotmail.com> 写入消息
news:0A88D47E-48D9-439A...@microsoft.com...
>