Alias and namespace conflict in Visual Studio Designer - c#

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();

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.

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.

Removing part of a namespace across entire project

I know that I can rename a namespace using Visual Studio 2010, but I need to remove part of a namespace.
namespace Xyz.Common.Utils { ... }
Renamed to
namespace Common.Utils { ... }
I need to drop the Xyz part, but don't see a way to use the VS refactoring tool to do this.
This is what I did a few times and you don't need any third-party tool:
Refactor the name in to something unique like: fjhfhchdbyegdrkoksodbc (if that's unique enough to you)Then do a global replace on fjhfhchdbyegdrkoksodbc. (including the dot) with an empty string.
There's no way to do that in pure Visual Studio, although there's an option when you select a word with the right button, then Refactor »» Rename in VS2010, this option requires you to select a symbol. In your case
namespace Xyz.Common.Utils { ... }
Xyz would be the symbol to refactor, but it cannot be renamed to an empty value.
Telerik JustCode can do the job instead:
http://www.telerik.com/products/justcode/features.aspx
There's also a Trial Version.

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

Label control in ASP.NET failure to pick a Label property

I am trying to use the Label control of ASP.NET as below:
public static bool addData(string storedProcName, string[] dynamicParamName, object[] paramVals, Label msg)
{
msg = "Recorded Added successfully";
cmd2.Connection.Close();
cmd2.Dispose();
}
However, I do not seem to get the "Text" property of Labels, as in msg.Text. Is there something I am missing here? Thank you.
Much as Chris Mullins suggested it looks like you are referring to the wrong type of label.
I am surprised that you aren't getting any comments of ambiguous names if you definitely have the System.Web.UI.WebControls referenced in a using statement. However, you should be able to fix it by either removing the line that you probably have saying "Using System.Windows.Controls" or by changing your reference to lable to be:
System.Web.UI.WebControls.Label lb = new System.Web.UI.WebControls.Label();
Or similar things.
Essentially it all looks like it boils down to ambiguity of the label class in your code.
Check that you have using System.Web.UI.WebControls; in your using section. Other than that you probably have a syntax error somewhere causing .net to not give you the intellisense.
Based on what you said in your comment it sounds like you may be working in a class library, if so make sure you include a reference to System.Web If you have a reference to System.Windows.Forms or a using System.Windows.Forms, you can remove them if you are not using them.
If you include both using System.Windows.Forms and using System.Windows.Forms then the compiler may not know which one you mean when you just say Label, in which case you would have to fully qualify it with all the namespaces.

Categories