Can I hide classes in the global namespace in C#? - c#

In a visual studio 2008 solution, I have two versions of the same class in two different namespaces.
In VB, if I do this:
imports MyNamespace
' ...
dim x as DuplicatedClass = new DuplicatedClass()
it uses MyNamespace.DuplicatedClass instead of the globally-namespaced DuplicatedClass. Meanwhile, in C#, doing this:
using MyNamespace;
// ...
DuplicatedClass x = new DuplicatedClass();
uses the globally-namespaced DuplicatedClass. To use the other version, I have to use MyNamespace.DuplicatedClass.
I realize this is a problematic setup, but I can't change it. Is there a way to prevent C# from seeing the globally namespaced class, or to specifically un-load it, or...? Given how many classes are in the global namespace, being forced to choose the namespace every time could get pretty time-costly.

Perhaps the best you could do is create a using alias:
//create alias
using defDuplicatedClass = MyNamespace.DuplicatedClass;
defDuplicatedClass x = new defDuplicatedClass();
The alias is file scoped. So you'd have to repeat it at the top of each file as needed, but perhaps that is better than repeating a namespace with every occurance of DuplicatedClass.

Related

VS 2015 Shared Project causes Type exists in both X assembly and Y assembly (CS0433)

I'm trying to implement a common attribute type and corresponding enum type to be included in AssemblyInfo.cs for every project, for use as metadata. All projects in my solution will require the attribute.
I have created a Visual Studio 2015 Shared Project and included these types.
Subsequently, I referenced the Shared Project in some base level assemblies, which contain no interdependencies. However, if I include two or more of these assemblies as references in larger projects, I receive the "type exists in both X assembly and Y assembly" (CS0433)
Am I implementing the Shared Project incorrectly? Is this a bad approach to solve the stated task?
This happens when you have two using statements at the top, which contain classes with identical names. There are two possible ways to deal with this.
Remove one (or both) using references and type the class names and types long-hand.
Example:
//using System.Data.SqlClient;
//using System.Data.OleDbClient;
var p1 = new System.Data.SqlClient.Parameter();
var p2 = new System.Data.OleDbClient.Parameter();
or
Alias both using references. You will still have to qualify your class names, but you can use the alias instead of full long-hand.
Example:
using Db1 = System.Data.SqlClient;
using Db2 = System.Data.OleDbClient;
var p1 = new Db1.Parameter();
var p2 = new Db2.Parameter();
I seem to have stumbled upon an answer. I had my Shared Project types declared under their own namespaces.
When I removed the namespace block from the code, the code compiled correctly.
I'm not sure if this a legitimate fix to the problem, but it seems to make the compiler happy.

C# How to use two different versions of the same namespace (i.e. Kinect 1.8 and Kinect 2.0)

In C#, I want to reference Microsoft.Kinect (v1.8) and Microsoft.Kinect (v2.0) in the same project. They are different DLLs but use the same namespace. I am able to reference them both with a renaming hack in the .csproj file (by calling them "Microsoft.Kinectv1" a "Microsoft.Kinectv2" respectively.
The problem still remains of how to specifically reference one or the other in code though, since they both have the same namespace. Visual Studio notices that the some class is used in both asseblies (v1.8 and v2.0) but I don't know how to specify one version or the other in code. I have read several posts about this but none of these posts went as far as showing specifically how to solve this issue. How would this be done?
In the properties window give each assembly a different alias (lets say you named them: assem01 and assem02);
At the TOP of the file you want to use the same namespaces write:
extern alias assem01;
extern alias assem02;
Note: These lines MUST be written before any other code in the file.
Now you can access the same namespace from both assemblies, for example:
var c1 = new assem01::ns1.ns2.myClass();
var c2 = new assem02::ns1.ns2.myClass();
For convenience, you may wish to give some aliases to the namespaces too, for example:
using assem01_ns2 = assem01::ns1.ns2;
using assem02_ns2 = assem01::ns1.ns2;
var c1 = new assem01_ns2.myClass();
var c2 = new assem02_ns2.myClass();
Hope this was helpful.

Error "CS0104: 'DataType' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.DataType' and 'CarlosAg.ExcelXmlWriter.DataType"

I am getting this error:
"CS0104: 'DataType' is an ambiguous reference between
'System.ComponentModel.DataAnnotations.DataType' and
'CarlosAg.ExcelXmlWriter.DataType'"
while running an ASP.NET 4.0 application. Can any one help me on this issue?
As described in the documentation on Compiler Error CS0104 you've got a symbol clash - there are two classes in scope of your source file which are both called DataType - one is in the namespace System.ComponentModel.DataAnnotations and the other is in the namespace CarlosAg.ExcelXmlWriter.DataType'.
You need to do one of the following to resolve this:
1. Explicitly provide the full namespace prefix on each usage, i.e.
System.ComponentModel.DataAnnotations.DataType and CarlosAg.ExcelXmlWriter.DataType:
var cdt = new CarlosAg.ExcelXmlWriter.DataType();
var sdt = new System.ComponentModel.DataAnnotations.DataType();
OR 2. Or use a using directive to alias the namespaces / types, e.g.
using SystemDT = System.ComponentModel.DataAnnotations;
using Carlos = CarlosAg.ExcelXmlWriter;
And then then identify the types with the namespace aliases, e.g..
var dt = new Carlos.DataType();
OR 3. You can also alias at the class level:
using SystemDataType = System.ComponentModel.DataAnnotations.DataType;
using CarlosDataType = CarlosAg.ExcelXmlWriter.DataType;
...
var myObj = new CarlosDataType();
OR 4. If you don't need to symbols from both namespaces, then delete the unused namespace from the using clause.
My preference would be for Option 2 - it makes it clearer to the reader that there is a namespace clash, without being too verbose (like Option 1 is)
Edit
Re: "I tried by giving full prefix but still I am getting error "CS0138: A
using namespace directive can only be applied to namespaces;
'CarlosAg.ExcelXmlWriter.DataType' is a type not a namespace"
(All relating to point #2, above). The error message refers to a situation like this, which isn't permitted in .Net (but is permitted in Java imports)
// i.e. This won't work, can't import at a class level unless it is aliased
using System.ComponentModel.DataAnnotations.DataType;
As per my answer, I would recommend that you alias the namespace, and then use the alias prefix to disambiguate between the 2 DataTypes
I had the same problem. The solution I used was to delete all using and make the references again. Worked perfectly.

Resolving an ambiguous reference

I'm trying to create a manager class to use with my charting tool, the problem is the tool I use, uses the same names for both a 3d and 2d charts which is resulting in ambiguous reference when I try to add the 2d library.. any ideas how best to resolve this?
For example,
using tool.2dChartLib;
using tool.3dChartLib;
BorderStyle is a member of both of these
I've tried casting the areas where I use BorderStyle. I suppose it could work if i just reference tool but then that would mean I'd have hundreds of tool.class lines instead of class
If the types with the same name exist in both namespaces, you have a couple of options:
1) If the number of types is small, create an alias for that type:
using BorderStyle3d = tool.3dChartLib.BorderStyle;
2) If the number of types is large, you can create an alias for the namespace:
using t3d = tool.3dChartLib;
Then in your code...
t3d.BorderStyle
You can use full type names, or create aliases:
using 2dBorderStyle = tool.2dChartLib.BorderStyle;
Use namespace alias
using twoDimensionLib = tool.2dChartLib;
using threeDimensionLib tool.3dChartLib;
I had similar problem that the class had ambiguous reference for the SAME namespace, so I deleted a specific Project (under Dependencies/{my.prj.name}.API) which had duplicated reference.
After that I referenced Project back with using of CTRL+.
Hope it'll work for you.
New reference of specific class

Alias and namespace conflict in Visual Studio Designer

I have a namespace conflict between two referenced assemblies:
i.e., I'm referencing Foo.A.Foo and Foo.Bar, so when I say I want Foo.Bar.Control, VS is trying to find Foo.A.Foo.Bar.Control
I can twiddle the Designer.cs code by adding new global:Foo.Bar.Control(), but as soon as I change anything, VS switches back.
I know there's something about adding aliases directly to the reference, I've tried but haven't managed to find the right combination (inline alias, using alias, reference alias).
Help?
"extern alias" may be what you mean, but I'm not sure what the designer will do with it, unfortunately...
I'm not even sure that's what you're after though - that's normally for two types from different assemblies with the same name.
You can write namespace aliases with a using directive, e.g.
using FooControl = Foo.Bar.Control;
but again, the designer is going to rewrite your code...
OK, this isn't the answer, but it's what I found for a workaround:
namespace FooBar
{
class FooBarControlHack : Foo.Bar.Control { }
}
So I can do the following in the Designer.cs :
this.fooBarControl = new FooBar.FoorBarControlHack();

Categories