Last post Jan 28, 2016 12:56 PM by gerrylowry
Jan 27, 2016 07:38 PM|Goalie35|LINK
In my code, I created a custom object named "File", based off of one of my database tables, which is named "File". Normally it's fine to use, however in one of my pages, I needed to also include "System.IO.File".
So now because of this, I'm receiving a " 'File' is ambiguous..." error. To get around it, I'm using the full object name, such as the following:
MyCompanyName.MyProjectName.Domain.File file = new MyCompanyName.MyProjectName.Domain.File();
Writing the full name out each time can be a pain. What's the correct thing to do in this situation? Do I just bite the bullet & continue to write out the full name? I'd prefer not renaming my object, since it matches the name of the database table.
Jan 27, 2016 07:51 PM|PatriceSc|LINK
You could use
Jan 28, 2016 12:46 AM|oned_gk|LINK
You can do like this
domain.File file = new domain.File();
Jan 28, 2016 02:46 AM|gerrylowry|LINK
(a) you could refactor every instance where "File" in YOUR "... .Domain.File" to something that will not collide.
Example: let's say your company name is Acme Manufacturing Company.
you could rename YOUR "File" to "AmcFile"
(b) you could use alias directives as suggested by PatriceSc and oned_gk above.
study the follow articles about aliasing options in depth:
https://msdn.microsoft.com/en-us/library/sf0df423.aspx "using Directive (C# Reference)"
https://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx "How to: Use the Global Namespace Alias (C# Programming Guide)"
Personally, i'd rather refactor,
Note: generally i avoid creating such problems is the first place. Of course this is not always possible, especially if one is using several third party APIs/frameworks.
Jan 28, 2016 03:16 AM|Klein Zhang|LINK
Do I just bite the bullet & continue to write out the full name? .
If you only use one of the namespaces in the class, there is no ambiguity and you can go ahead and use the type. But you use both of the namespaces, you will either have to fully qualify the usages, or use namespace/type
aliases to disambiguate the types. Like this:
using ERPUtils = MyCompany.ERP.Utilities;
using BCUtils = MyCompany.Barcode.Utilities;
public void MyMethod()
var a = ERPUtils.Method();
var b = BCUtils.Method();
I'd prefer not renaming my object, since it matches the name of the database table.
You could rename the name fo the database table, then you could rename your object, in my experience, i think you should rename both of them to something more informative.
I hope it's helpful to you.
Jan 28, 2016 12:56 PM|gerrylowry|LINK
in this reply, i am introducing three more alternative solutions:
as i mentioned above, (i) i prefer to prevent name collision by choosing a name like "AmcFile" for my identifiers, and (ii) this is not always possible, especially when one needs to use one or more third party API or framework.
in case (ii), one may not discover the collision, as in your O.P., until compile-time.
Your solution (my preference because it offers substantially more clarity), was to use a fully-qualified identifier name.
intellisense* takes away much of the effort.
The 20 characters of "System.IO.File.Copy(" can be typed with as few as 11 keystrokes:
NOTE: i compile almost after every line of code, so with the help of the compiler i would detect a collision very early.
imho a FQIN is always better; when forced to use a FQIN or aliasing, i always prefer to use a FQIN.
c# allows us to split lines; VS also permits wrapping long lines with/without a visual glyph.***
MyCompanyName.MyProjectName.Domain.File file =
with intellisence, as demonstrated above, typing a FQIN is not a big deal.
in your case, where you've already written a lot of code, you can always use search/replace.
custom code snippets are not difficult** to code; AmcF tab tab could expand to "MyCompanyName.MyProjectName.Domain.File."
* https://msdn.microsoft.com/en-us/library/hcw1s69b.aspx "Using intellisence"
** https://msdn.microsoft.com/library/ms165393(v=vs.100).aspx "Creating Code Snippets"
http://blogs.msdn.com/b/visualstudio/archive/2014/01/15/visually-creating-snippets.aspx "Visually Creating Snippets"
*** https://msdn.microsoft.com/en-us/library/ms165339.aspx "How to: Manage Word Wrap in the Editor"