Namespace & Class Conflict - c#

I'm facing a problem with a conflict between the DateTime class and a namespace for some unknown reason was also called DateTime.
Assembly CompanyDateTime has namespace Company.DateTime
My Application is in the namespace: Company
the problem is that everytime I need to use DateTime class, I have to explicitely say System.DateTime is their any way of getting around this?
Is is possible to say SomeRandomStuff = Company.DateTime and have DateTime always be System.DateTime
Note:
I need to reference this Assembly in my application eventhough I do not use it because some Assembly that I need actually uses this class.
I can use an entry on the app.config file to identify a dependent assembly but I cannot do that since company policy is against it and all referenced assembly needs to be in the output folder.
Deployment goes through a build server
Possible Resolution?
Is is possible for CompanyDateTime to get automatically deployed to the output folder without adding it in the reference?

Question : the problem is that everytime I need to use DateTime class, I have to explicitely say System.DateTime is their any way of getting around this
Answer : Already answered above - use an Alias eg
using CompanyDateTime = Company.DateTime;
using StandardDateTime = System.DateTime;
Question : Is is possible for CompanyDateTime to get automatically deployed to the output folder without adding it in the reference?
Answer : Put this dll in the application root folder and create a postbuild event that copies this to the output folder. You can use the regular DOS COPY command here.
Refer link for postbuild event details

Yes, use a using directive at the top of your code files that reference it like this.
using SomeRandomStuff = Company.DateTime;
Edit: you may also need another one to resolve the ambiguity:
using DateTime = System.DateTime;

Give the assembly an alias (the default is global, set yours to something else). You can set this in Visual Studio's property window when you select a reference, or using a compile switch if you're manually compiling. The compiler will only resolve things in the global alias, unless you specify an external alias at the top of your code file (extern alias myalias).
Note, this is different from the namespace alias others have mentioned. With this you should simply be able to use DateTime to refer to the System.DateTime, rather than using another name. If you need to refer to the other one later, you'd need to specify myalias::Company.DateTime...

Use an alias.
If you want to use System.DateTime without having to use System, then try:
using SysDate = System.DateTime;
Then, just reference it like you would a class:
SysDate mySystemDotDateTime = new SysDate();

Related

How to access a class which is in a folder c# (wpf)?

I'm building a WPF application. I'm almost finished with one part of it, and now I want to clean up the code a bit. English is not my native language (so sorry if this gets marked as a duplicate) and I couldn't find anything about this, so I hope someone can help me out.
If I have public voids in a class called 'class.cs' and this class is in a folder called 'classes', how do I access the class so I can use its functions?
At the top of the file where you want to use the class include a using statement.
using [namespace];
Then you will be able to instantiate the class
Class testClass = new Class();
testClass.Method();
If you aren't sure what the namespace for your Class class is, then you can write the line to instantiate the class and then use the intellisense (assuming you are using a development ide like Visual Studio) to automatically include the reference. Alternatively, you can look at the top of your class.cs file to see what namespace has been declared.
check what namespace it's in , then what the class name is and call it as
"Namespace.class"
in case that you can't see it still , check if the class is public and the class.cs properties have the build property set to compile
I'm not sure from where You are trying to access this class. But I think the easiest way of doing it probably will be just typing your class name in place where You are trying to use it and press 'ctrl+.' which should show You context menu with options to add using statement for this class.
If You are trying to access this class from other project in solution then above way will only work with VS2017(because it will also add missing project reference) otherwise You need to add reference for this project and then add missing using statement for your class(in your case 'using ProjectName.classes').
The folder you are talking about is called a namespace. You first need to import the namespace and then you can use your class.
*Normally the IDE will prompt you.

How do I find which assembly I need to reference in order to use a certain namespace?

Ok, so this is a common occurrence for me: Through docs on MSDN, or other people's code snippets, I've found a class or namespace I want to use in my project, but I don't know which assembly I need to add in order to make that class or namespace available.
I've found lots of information about how to find out what namespaces a dll exports, but not much for going in the other direction.
I'd really prefer not to have to manually search through every dll on my machine. Is there a list/tool/resource somewhere that I can go to for this information? For the standard .NET/WPF classes at least?
You can search for the class on msdn.microsoft.com/library and it will list the namespace there. For example, for StringWriter:
StringWriter Class
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Go To View --> Object Browser and Choose All Components from the browse dropdown box. And search your name. And you see the results...

Moved a class to a different namespace, is it possible to add an implicit using to my files?

I moved a class from one namespace to another, and now I have over 2000 errors to go through.
All the errors are related to the class that I moved, is it possible to implicitly or globally add a namespace to all my files somehow?
Or is the only way to manually go and fix each error?
I have ReSharper, but I am weary of making a global change and not even sure if Resharper go fix this ONE issue in all the files?
With ReSharper you can right-click the class name, select Refactor from the popup menu and select Move... select Move Type To Another Namespace. ReSharper will change the namespace and correct all the files referencing the new namespace, an option if you can rollback your move.
Are you implementing the using statement on your classes that are calling this class?
If so, just use the search and replace for the entire solution from the old namespace to the new name. It will do it with. If they are in seperate projects in the same solution, you will have to add. a reference to that project.

I couldnt use the library that I have already imported as references in my project

I am writing a program for computer vision in c# that part of project SIFT is needed; I downloaded the MEMO library for SIFT :
http://www.nowozin.net/sebastian/tu-berlin-2006/libsift/
In my project I added the two libraries ****libsift.dll & ICSharpCode.SharpZipLib.dll** ** as references in to my projects.
Now that I want to mention libsift on top of the code " USING libsift" it doesnt recognize it;
even more if I wanna make a instance of such class atuo detect doesn't recognize it.
It is weird because I could see the functions of this library in object browser.
I couldnt use the library that I have already imported as references in my project although I could see the functions in object browser? What could be the problem?
(I have c# .Net VS2010 in windows.)
It looks like they're in the root namespace - not in a namespace called libsift. If that is the case, you don't need a using libsift; directive - just try accessing Keypoint etc directly. Or possibly global::Keypoint. It should be noted that dumping types into the root namespace is frowned upon - it makes it hard to avoid conflicts. But that is the fault of libsift.
The other possibility is that you've changed the "Aliases" property on the reference; that should be "global". You can see this in the property panel when you select an individual reference in the solution explorer - alongside things like "Copy Local", "Specific Version", etc. If it is anything else: change it back to "global", or learn about extern aliases.
Tested locally (the types are in the global namespace):

C# resx file not working after changing namespace

I am working on an existing windows form application. I only need to make few modification, during those changes i came across a scenario where i need to rename an existing namespace aswell. Now after renaming that namespace my project is not running. Although it compiles fine but breaks at the following line of code
ImageList il = new ImageList();
ImageList = il;
il.Images.Add((Image)new Bitmap(GetType(), "Images.ImageFileName.png"));//when this line is executed a dialog box appears and says that "there is no source code available for current location"
Now since i am new to .net world so i study this problem and figure that it arises due to change in namspace. I also trying renaming the namespace in the Resources.Designer.cs
but it also didn't solve my problem
Thanks All.
I find the solution by myself. The purpose of posting solution here is that may be some one else benefit from it.
Infact its a sort of carelessness from my side because i forget to change the value of Property "Default NameSpace" this property contains the old namespace that's why my resource file alwasy point to old namespace
Right click on Resources.resx, select Properties, find Custom Tool Namespace, set the namespace that you need.
Also, why don't you reference your resources in the typed manner like Resources.ResrouceName.
Here is a quick tutorial how you can setup your resources so you don't have to cast and think about namespaces: http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.80%29.aspx
try getting rid of that second line, ImageList = il;. what it's doing is replacing a class with an instance of a class. not good.
I have also came across this issue. I had to rename the namespace of my application. After that the designer was ignoring all resources, although they were appearing after building a solution and running the application.
I found that even though I had renamed the namespace, it wasn't replaced in Application Properties under "Default Namespace" textbox. I have changed the old namespace which was still appearing there with the new namepspace and it worked properly afterwards.
That specific overload of the bitmap constructor combines the namespace of the given type with the string name of the resource and looks for a match in the assembly manifest. So in this case it would be the namespace of your class + ".Images.ImageFileName.png". I'm guessing that does not exist. Change the namespace of the Resources.resx by right clicking on it and selecting properties, do a Rebuild and see if it works now.

Categories

Resources