VS 2013. .NET4, same project, Namespace could not be found - c#

Running VSPro 2013 on a x64 Win7 machine, creating a C# Console Application called PROJ, using .NET4 and using a class library I build IN THE SAME PROJECT. Class lib is called CHILD1 in CHILD1NAMESPACE. In PROJ, I have using CHILD1NAMESPACE; and it works!
I then created CHILD2, in CHILD2NAMESPACE... IN THE SAME PROJECT... with the exact same settings as CHILD1 (.NET4 / Class Library).
using CHILD2NAMESPACE; gives me an error! The Type or namespace CHILD2NAMESPACE could not be found...
I've searched the web and most resolutions include switching frameworks because the class library and parent are using different frameworks. In my case, they are all using .NET4 (not client) and are within the same Project. I've restarted VS several time... cleaned, re-built etc.
Any clues would be appreciated.
Thanks
-Ed

It looks like you created two projects (csproj) inside one solution.
In order to make one project access the other one you must add a reference between the projects.
In orde to do that, right click over the the project that is not building and click on "Add reference" you can choose the solution and select the project being referenciated.

Related

Converting a C# class I created inside a project to a separate reusable class

I'm not new to C# programming, but I suppose I'm new to programing "the right way" in C#. I've worked in C on embedded devices for years and have written desktop apps to support them. First in VB6, then in C#.
I recently started making better use of classes for reusing code (and for instantiating more than one instance of the class in a program). For example, I "wrapped" a UART interface with some additional functionality so I can use the same code for multiple ports by creating an instance of the class for each one.
It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.
I'm sure there's a way to create it such that I can just reference it like everything else with either a "using..." reference at the top of the program or with a "Project | References..." checkbox. But for the life of me I can't find a good learning journey for this.
Any direction would help.
You want to create your reuseable class in an assembly - this is the equivalent of a dll from your C experience.
To create an assembly, have a separate project of type assembly (instead of exe) . You can reference the assembly from other projects. If your project is in the same solution you can reference the project, otherwise you can reference the compiled assembly.
C# uses a packaging system called Nuget, so you can package your assemblies into "Nugets" which you host in a Nuget Server. You can then use tooling to discover and import these.
Please create a Class Library project and include your class into that project. Make sure your class is public. Once you build this project you'll get an assembly which can be referenced from other projects. See Tutorial: Create a .NET class library using Visual Studio
There are different ways of referencing it.
You can have the class library project in the same solution as the main project. In this case you should add a project reference.
You can copy the compiled *.dll file to some folder in your solution (e.g. Lib) and add an assembly reference.
If this assembly is to be used in multiple projects please consider creating a NuGet package with this library and pushing it to some repository. Then other projects can add a package reference to this package.
Details:
How to: Add or remove references by using the Reference Manager
Install and manage packages in Visual Studio using the NuGet Package Manager
It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.
Well, it isn't the best practice but (unfortunatly) still a common behavior. So don't worry to much about it.
What you could do to improve it place the file (and other reusable parts) in a seperated csproj.
For example name the project of the type class library and name it VinDag.Tools. Within the project create a folder UART and place the wrapper there. The namespace of the wrapper would then be VinDag.Tools.UART.
From know on you can just reference the class library instead of renaming the file. It's not necessarily required to be the same namespace as the project.
From there you can start considering (private) nugets. This would prevent you from copying files/csproj around.

How to use class from other project in visual studio C#

I have a Xamarin.Forms project and a C# console app project. I want to use one class from the console app in my Xamarin.Forms project.
I added the console app project to the solutions explorer of the Xamarin.Forms project.
Unfortunately, I can't figure out how to use the class from the console app in one of the Xamarin files.
I always get the error message:
The name 'MyClass' does not exist in the current context.
I tried to press alt+enter to show potential fixes but it does not offer me the option of importing/using the class.
I also wrote manual using directives in various forms but it still does not seem to make the class accessible.
The only way I was able to use the class was by adding the class directly to the Xamarin project by adding it with add->existing item. The problem with this is that it imports a copy of the class. Since I'm still working on the class within the other project the added class is fast outdated and I have to manually copy its contents over.
How can I use a class from an external project without making a copy of the file?
Instead of access class from console app(its exe) try creating new reusable library add that class and use in both projects also you can write wrapper class in both projects
Try to add a reference to the second project in your first project. To do this, right-click on your project, select Add Reference then select the project in your solution. Once your main project references the second project, then you can access its public types.

How to manage project with signed assembly in .NET?

Visual Studio 2008(C#).....I have 3 project in one solution(win application). In every project I've been signing using project properties signing tab to one MySigned.snk. How to manage build configuration so my project doesn't error on every build?
I use [assembly: InternalsVisibleTo("MYproject1, PublicKey=.....")] properties so the other project class can access internal class of base class on other project.
Error becasuse I used backgroundprocess to update every form on my project.But on development environment what suppose I have to do so The project safely on during development? thx before...
I use [assembly:
InternalsVisibleTo("MYproject1,
PublicKey=.....")] properties so the
other project class can access
internal class of base class on other
project.
This is not related to the issue.
In every project I've been signing
using project properties signing tab
to one MySigned.snk. How to manage
build configuration so my project
doesn't error on every build?
I assume it is because the assembly versions are auto-incrementing:
Use project references always. This way, you will make sure everything is built against latest version numbers
Removing * from the AssemblyInfo's assembly version attribute will help here but it is advisable to keep it there.

Namespace in Referenced Project Present in Autocomplete Before Building, but Causes Compile Error After Building

I have a class library project which uses a namespace (e.g., "Cosmos.Creator.Util"). I then create a solution and windows forms application to test the library. From the windows form application, I add a reference to the library. So now I have two projects open in visual studio, a class library and a windows forms project. The forms project references the library.
When I edit my form's code, code autocompletion works correctly for the namespace that I use in the library. E.g., if I type "using Cosmos." I get autocomplete options like "Creator". But now if I build my solution, all of the "Cosmos" are red-underlined with the compile error: "The type or namespace name "Cosmos" could not be found (are you missing a using directive or an assembly reference?)".
For the purposes of the form application test, I placed my library code into a folder CosmosFormExample\Cosmos. When I check the reference from the form application, the reference is to CosmosFormExample\Cosmos\bin\Debug\Cosmos.dll, so that looks okay. I looked at the GUID referenced in the solution file and it matches the GUID of the project file Cosmos.csproj.
What has happened? How has the build caused my forms application to forget about the Cosmos namespace, despite the fact that it is still referencing the library project? Thanks much in advance.
Are you using VS2010 & .NET 4? If so you're probably using .NET 4 Client Profile instead of full fledged .NET 4. Go to project properties and check your Target Framework.
for more info: http://msdn.microsoft.com/en-us/library/cc656912.aspx
you need to check the framework you are using and the framework yout library was compiled for...

WCF - Cannot resolve [WebGet] symbol - what am I doing wrong?

I am working on a REST WCF project and when I implement the following code, it complains that it can't resolve the WebGet class? What am I missing?
I tried importing the System.ServiceModel.Web namespace but it can't find it even though I referenced it. The "Web" in System.ServiceModel.Web does not register when I register it in a using statement on top of my code.
Basically, what do I need to implement such WCF REST concepts like WebGet, WebInvoke, UriTemplate, etc?
UPDATE: After some feedback and thinking about this a little bit more what I've done, it seems that the DLLs (System.ServiceModel & System.ServiceModel.Web) do not come up via the 'Add Reference' window when I go to add a project reference. When I first started the project, FYI, since these assemblies did not come up at first, I went 'searching' for them, and copied them to a temp folder so I can reference them and thus, I guess I am having the resolve issues. So, now that I am at this point, how can I get my VS to recognize/register these WCF REST DLLs? Thanks!
UPDATE: I believe I am update-to-date on everything: developing on VS 2008 SP1 - I try to download the latest SPs, downloaded the REST Preview 2 Starter Kit, developing against 3.5 Framework, trying to create a WCF REST layer to ultimately be consumed by Silverlight 2 client.
This is what I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using UtilityClasses;
using Microsoft.ServiceModel.Web;
using Microsoft.Http;
namespace WcfRestService
{
[ServiceContract]
public interface IRestService
{
[OperationContract(Name = "Add")]
[WebGet(UriTemplate = "/")] // ** can't compile here **
int Add();
}
}
Any advice will be greatly appreciated it.
You need to reference the System.ServiceModel.Web DLL.
Right-click the 'References' folder in your project and choose 'Add Reference...'. Scroll down to System.ServiceModel.Web and click 'OK'.
Just a one thought, you might be targeting your project to .Net Client Profile which exposes limited namespaces. you may need to check the target framework setting at your project properties.
I have faced that with a WCF project not finding System.ServiceModel.Web untill I changed the default framework settings.
HTH
This happened to me too.
I did this:
Delete System.Service.Web from References
Build
Clean Project
Add System.Service.Web to References
Build
..and VS found it??
In "project properties" make sure your "target framework" is set to : .NET Framework 4
and not: .NET Framework 4 Client Profile, or any lower .NET version.
Also, if possible use VS 2010.
--DBJ
right click on the project name and choose Properties.
change the target framework to .NET Framework 4.
right click on the References and choose Add Reference.
And then you can see System.ServiceModel.Web.
By default the target framework is .NET Framework 4 Client Profile,
so you cannot find the System.ServiceModel.Web.
I had the same problem.
I have added this missing reference:
System.ServiceModel.Web
and this code line:
using System.ServiceModel.Web;
and all got solved! ;)
using System.ServiceModel.Web;
In my case my project was building despite this warning in the designer view of the service class. Not really a big issue, but still pretty annoying. Realised it was just ReSharper playing up - it hadn't updated its internal cache when the reference to System.ServiceModel was added automatically by VS when I added a new WCF Service. I turned off real-time code analysis in:
Tools -> Options -> Resharper Ultimate -> Options -> Code Inspection->
Settings -> Enable code analysis
This restored the built-in VS code analysis, and problem was fixed straight away.
If you'd prefer to keep using ReSharper code analysis, clearing the cache in:
Tools -> Options -> Resharper Ultimate -> Options -> Environment ->
General -> Clear Caches
may also sort the issue.

Categories

Resources