Unable to create object - c#

There is a .Cs file in one of the projects in my application and i want to create an object of the class(in some other project inside the application) that is inside the .cs file, i tried to add the reference of that particular project but it is giving Circular Dependancy error and i am unable to create the object.

Break out common code to a third assembly and reference that one in the other two projects.

You get a circular dependency becuase the project in which the object reside already references the project that you want to use the object in. This is probably a design problem as well.
One solution is to create a new project (Common) that both projects can reference.

Related

What is the proper way to link classes in separate projects?

I have a Solution which consists of two projects - the Manager and Viewer. Naturally, both use the same classes, e.g. Manager is used to edit data in SomeItem class instances, while Viewer is used to display it's data.
I have all class definitions in the Manager project. To use them in the Viewer project, I created the same directory hierarchy in the Viewer and linked all classes with build action set to "Compile".
But now I'm getting tons of warnings like this:
The type 'SomeItem' in 'Manager\Classes\SomeItem.class.cs' conflicts
with the imported type 'SomeItem' in 'Viewer, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null'. Using the type defined in
'Manager\Classes\SomeItem.class.cs'.
What is the best way to use the same class in two projects while having it in a single file?
I would suggest that you create a separate project with the classes you want to have in common, set application type as Class library, after doing this and compiling it you will be able to add it as a reference to both of your other projects by right clicking on references and adding it as a project reference.

Copy an existing service reference from one project to another

I found a way to copy a DLL reference from one project to another using VS Power Tools. Apparently it can't copy service references. Is there a way to do this manually?
The problem is that we cannot access the service right now. So we have to build the code without being able to add the service reference conventionally.

Why I need to remove and add dll reference every time in Visual Studio .NET

I am adding one dll reference to my project. Whenever I modify the dll, I copy the dll the same folder overwriting the old one. But my compiler starts giving errors on the dll methods. Every time I have to remove the dll reference from the project and re-add the reference to build it.
My question is, why I need to remove and add reference every time? .NET should take the new dll automatically?
As others have stated, this appears to be a versioning issue. An alternative to adding the project to your solution (if you don't want it in there for some reason, or you have a requirement to reference the dll directly) is to modify your reference so that it doesn't look for a specific version.
Find the reference to the built assembly in the 'References' project folder
Right click on the reference
Change 'Specific Version' to false
hth
If you have one project depending on the other, put them in the same solution and add the dependant project as a "Project Reference" rather than referencing the DLL directly.
Alternatively, if you want to use an assembly in multiple places, you can create a NuGet package. You can place these in either public or private feeds - and then add the dependency using NuGet.
If you do either of these the dependency will be managed for you.

Adding this project as a reference would cause a circular dependecy

I'm working on an MVC 3 project. I was told to get all the models and viewmodels out of the projects and put them in a class library so that they can be referenced from different types of projects. However, now that I've transferred all the viewmodels and models from the web project to a class library, and removed all the references to the web project, I cannot set reference to the class library from my web project with the reason stated in the question title. WHy is this happening? In my class library I'm not referencing the main project anywhere!!! Any suggestions? Thanks a lot!!
Experienced this earlier. Check the project that you are going to add if it has the reference to the project you are adding in it.
Example: Project A with reference to Project B. Then in Project B, you're adding Project A as reference.
well this usually happens for a reason, and this is that there is a cirrular reference,maybe not a direct one but an indirect one (through third project, how many projects do you have in your solution?).
In your library project remove all other projects references from solution, and try it again.
good luck
almir

C# solutions : using one "Globals" project for external dll's?

(Sorry for might be a trivial question , I'm coming from Java & Maven , and still haven't wrapped my mind around C# dependencies )
I want to use log4net in all my projects. Since I don't want to add the dll to all the projects , I've created a "Globals" project , add a reference to log4net.dll in it , and referenced from all the other projects to the "Globals" project .
However , I can't seem to access the log4net classes from any other project .
using Globals.log4net;
Doesn't seems to work either .
What am I doing wrong?
If all you did was reference the DLL, then all you have done was get a copy of the DLL with every reference to your Globals project. You are still not using the library.
What I would normally do would create an ILogger interface, implement it using log4net in the Globals project and use that implementation in the other projects (plus a mock implementation for tests).
I'm afraid that's not how it works.
You have to add the DLL to all projects you want to call it from.
If you were only using a couple of functions in the DLL, you could create functions in your Globals project to call through to it.
log4net doesn't 'live' in Globals simply by the reference.
My 1st inclination would be to have all of your projects just reference log4net, it clarifies that there's a dependency there no need to hide it in another project.
However, if you do have common logic shared across your classes you could have a "Global" or "Common" class which includes references to shared libraries. To reference those libraries just add the using of the target namespace.
In other words, no matter if the reference is within the same project or another reference project, the using statement will be the same.
For log4net i believe it should just be:
using log4net;
The other way to add the proper reference would be to type one of the class names somwhere in your code ( Logger ? ) and then invoke the helper menu with "CTRL+." or by simply expanding it, this will have the option to add the proper using statement.
That system won't work. You'll have to add the log4net dll as a reference to all the projects. Or create proxy classes, which is much more work.
Read up on the GAC (Global Assembly Cache), this a central storage for DLLs that are shared across projects... thats where I put my log4net DLL. You can then simply add the reference to it in your .config file forevery project you need to use it in without adding the DLL to the projects themselves.
This is a good place to start: MSDN: Working with the Global Assembly Cache

Categories

Resources