I know that one shouldn't include using namespace declarations in header files as they make items available to the whole project and that might have a bad result. But so far I have been unable to find info on whether a "#using" directive is allowed in header files. Can I use it in a header file like in this example?:
Here's my header file with function forward declaration:
#using <System.dll>
System::Security::Cryptography::X509Certificates::X509Certificate2^ GetCertificate(int Index);
If I can't, what is a workaroud for inclusion of System.dll, on which X509Certificate2 is dependant?
The #using directive does something completely different from the using namespace keyword. It is the equivalent of the #include directive as used in native C++ code. It tells the compiler to load the type definitions from the assembly metadata.
And no, you should not be using it. It is pretty broken in recent VS versions, the exact version of the reference assemblies you use in your project matter a great deal since .NET 4.0. Lethal since 4.5 when you get the wrong one. Besides, System.dll is always referenced in a properly configured C++/CLI project so #using it again makes no sense.
Instead use Project + Properties, Common Properties, References. And be sure to use a project template from the CLR node when you start a new project.
Related
When I try to Imports System.Web.Script.Serialization, I get an error in VB 2010 that says:
Warning: Namespace or type specified in the Imports System.Web.Script.Serialization doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
Not sure why I can't import it.
You might be missing an assembly reference to System.Web.Extensions.dll. Add this reference to your project, then try again.
Generally speaking, when you encounter this issue, go to the .NET API reference page of the type you want to use — for instance, JavaScriptConverter — and look out for the Namespace and Assembly hints (make sure you're looking at the page for the .NET framework version that you are using):
Namespace: tells you what to put in the Imports directive.
Assembly: tells you what assembly you need to reference in your project (e.g. go to Solution Explorer, locate References, and select Add Reference… from the context menu).
Also make sure to check the "Target Framework" in the project properties. If you are targeting a "Client Profile" framework then the assembly System.Web.Extensions.dll will not be available to add as a reference to your project.
If you try to use Windows as part of your own namespace in Metro-style apps (e.g. MyCompany.Windows) it seems that it throws compiler(s) off, because they start looking for WinRT stuff under your namespace rather than Windows.*.
For example, if you create a blank Windows Metro style app named App1 and rename App1 namespace to be App1.Windows and try to compile it you'll get an error:
The type or namespace name 'UI' does not exist in the namespace 'App1.Windows' (are you missing an assembly reference?) F:\temp\App1\App1\obj\Debug\App.g.i.cs
the top of the generated file looks like this:
namespace App1.Windows
{
#if !DISABLE_XAML_GENERATED_MAIN
public static class Program
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
static void Main(string[] args)
{
Application.Start((p) => new App());
}
}
#endif
partial class App : Windows.UI.Xaml.Application
and the problem is that compiler starts looking for Windows.UI under App1.Windows.
Now this is nothing new and if you'd name your namespace App1.System before you would probably get into a similar kind of trouble at some point. The problem is that quite a lot of WPF/Silverlight/Windows Phone code out there uses MyCompany.Windows.Xyz namespaces and is probably susceptible to this kind of trouble.
Is there some guidance from Microsoft (can't seem to find one) as to what to do with these namespaces? Or maybe there are some plans on resolving this in future versions? Or am I just missing something?
I looked at this issue and we should be using global:: in the generated code. We have fixed this on our end and will be available in next release of Visual Studio.
This is a known behavior in the C# language specification. Basically whenever a namespace appears in a "using" clause, the system treats all the elements in the namespace as if they might be part of the using namespace. There are two workarounds: Remove the name "Windows" from the inner nodes in your type or remove the "using" directive.
This is unfortunately not a winrt issue, you can get the same thing to happen with pure CLR applications.
I created a new Metro Split App in C++ using VS2012 on Win8 (both RC). Everything compiled and worked out of the box. I then changed went through and changed the generated namespaces to my own. After some trials and tribulations, I got everything to compile with no warnings, errors, nor messages. The app (as it comes in the project template) runs fine.
However, if I try to edit either of the generated xaml files (ItemsPage.xaml or SplitPage.xaml) I get a "Markup error" on the first line:
The name "LayoutAwarePage" does not exist in the namespace "using:A.B.Product.Client.Common".
The definition of the class is:
namespace A{ namespace B { namespace Product { namespace Client { namespace Common
The code compiles fine, and runs fine. This only happens in design mode.
UPDATE: I added a new xaml file and (after fixing up the namespaces again) everything worked.
Please let me know if any additional information is needed.
The name of the WinMD file produced by your project must be some prefix of the namespaces in which the public WinRT types are defined. Given that your type is in the A.B.Product.Client.Common namespace , the WinMD file must have one of the following names:
A.winmd
A.B.winmd
A.B.Product.winmd
A.B.Product.Client.winmd
A.B.Product.Client.Common.winmd
The public types must also be defined in the WinMD file with the longest prefix that matches the namespace. So, if you have both A.winmd and A.B.winmd, the type A.B.MyClass must be defined in A.B.winmd.
So, why does your code work at runtime but not in the designer? The naming rules for public types only apply to types defined in Windows Runtime components (for C++, DLL files), not for applications (EXEs).
However, to be able to instantiate your user-defined types (including LayoutAwarePage), the designer will load your project's EXE as a DLL, so the naming rules must be followed.
I had a similar bug, but then I closed VS, deleted the .suo, and reloaded the project and everything worked just fine.
This is regarding creating the framework in iOS, as I have a bundle of unity which I want to create a framework, with data with-holding and linking library from Unity as libiPhone-lib.a. So without adding any library in the bundle target, the compilation works fine, if I include libiPhone-lib.a file, it generates a warning as:
warning: implicit declaration of function 'UnitySendMessage'
The UnitySendMessage is a function which is being called from the dedicated libiPhone-lib.a framework.
Any suggestions regarding this concern will be really appreciated.
Thanks.
This error means that, in the file where it occurred, UnitySendMessage was called without the compiler having seen a declaration for the function. You need to edit the file and #import the header that contains UnitySendMessage (or #include if it is a .c file)
.
The Unity folks seem to have neglected to include a header declaring this function. You can either declare it yourself, or just ignore the warning.
I'm getting a compiler error saying that "Acme.Business.User" is not defined.
I have a class library project called "Acme.Business" that has "Acme.Business" as the assembly name and root namespace as well. None of the classes use the "Namespace" keyword, so they all should exist in the namespace "Acme.Business".
I also have a class library project called "Acme.Web" that has a project reference to "Acme.Business". Again "Acme.Web" is the project name, assembly name, and root namespace.
Here's the weird part. If I add a class to "Acme.Web" I can type "Imports Acme." at the top and see both namespaces appear in intellisense like you'd expect, but if I try to do "Dim x as New Acme.Business.User" then "Business" doesn't show up in intellisense and I get an error saying "Acme.Business.User" is not defined.
I can't see what I'm doing wrong! Please help. Thanks.
I think you may be misunderstanding how project default namespaces work. The default namespace is a project file setting that simply tells Visual Studio what namespace to add to each file when you add a new class file to the project. If you have removed all of these namespaces from the code files then your types do not exist in that namespace.
This means that all of your types in the Acme.Business assembly live in the global namespace which is probably not what you want. In order to get the desired behavior you will need to add the namespaces back into your code files as that is the only way the compiler will create type names with that namespace.
OK, I figured out that the behavior I was seeing was because I was declaring namespaces within the code in Acme.Web the way I was used to in C# which is to fully qualify it, ie. Namespace Acme.Web.UI.WebControls. I didn't realize that in VB.NET it's building on top of what was specified for the root namespace. I removed the portion that was specified in the "root namespace" setting of my project and it started working. So my namespaces in code for Acme.Web now look like Namespace UI.WebControls.