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.
Related
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.
I would like to use some of methods from my c# console app project ("EPS logic") in UWP project ("EPS view") in same solution.
I tried that to add reference- Calling methods from different Projects in one Solution.
But when I tried to I got two errors:
Cannot add reference to project "EPS Logic"
and
The method or operation is not implemented.
Additionaly I got some warnings like this about all classes from my EPS logic project:
warning DV2002: Class 'SequenceMaker' is not mapped to any Dependency Validation diagram
But I don't know if it's part of that problem. Should I not try to add anything to UWP project in first place?
You will not be able to reference a console app from a UWP project; they are not compatible! If it is really just logic, you should create a .NET Standard class library or a UWP class library and put your logic there. You should be able to reference that just fine.
If you are wanting a console app and a UWP app with shared logic, split that logic out into a .NET Standard class library, then reference that from both the console and UWP projects.
Is it possible to use the TreeView Class in a class library.
I want to make a method where I pass a Treeview from windows form , I tried to import System.Windows.Forms but it is not found and I cannot find any nuget package to install.
I want to do something like this:
public void MyMethod(TreeView tree){tree.Nodes.Add("Something");}
Thanks.
In Solution Explorer:
Right click on the class library project, select Add > Reference. On the left sidebar choose Assemblies > Framework. Check System.Windows.Forms and click Ok.
I would caution against adding this reference in your existing class library unless you want it to be specifically only for WinForms. Perhaps a cleaner approach would be to create a new class library, say .WinForms, and add a project reference to the original project and a Framework reference to System.Windows.Forms as I described above. This would keep the rest of your code separated out from the WinForms code in the base project, and then it could be used in other contexts without WinForms.
Hi I have console application which is written in VB.NET. For this I added c# class library with existing solution. I wrote code in c# class library. Now when I try to run, it is going to VB.NET console application by default. I am not able to run my c# class library.
For clear understanding lets call VB.NET project as classVB and class library as classC#
I tired these methods to make it work :
1) Added classC# reference to my classVB project and made classVB as startup project. I used using statement also to refer to my classVB project as Using classVB. And I put break point in my classC#.But still it is pointing to classVB project
2) Tried to made classC# as startup project( Even I knew this doesn't gonna work). For this I am getting usual error which says "A project with an output Type of class library cannot be started directly."
3) Right click on solution and start up project option -> Single startup project and selected classVB.
But none of this is working. It is always pointing to my classVB project even after I put breakpoint on c#class library code.
Its the first time I am working on library class, so any help on this would be appreciated.
You can't directly run a class library. It is not executable. You can reference the code from it in your VB project, but a class library can never run by itself. To access a public method in your class library from VB, refer to it by Namespace.ClassName.MethodName.
Change the compilation output of the library to the same path where you added the reference in the VB.NET application. Generate the library and check the build is successful.
Do not forget to change the library class to configuration debug in the solution to generate the PDB file that will allow you debugging. Put a breakpoint and try again.
I have a C# console application in Visual Studio 2010. It has a Main() method as well as a bunch of utility classes. I'd like those utility classes to be available to other solutions. From reading online it seems that I need to compile it as a Class Library (DLL). So here's what I did:
Went in Visual Studio to "Project > [ProjectName] Properties > Application" and changed "Output type" from "Console Application" to "Class Library"
Rebuilt; ProjectName.dll was created in bin/Debug.
Created a new Console Application
Solution Explorer > Add Reference > browse to ProjectName.DLL, select it.
However, neither IntelliSense nor the Object Browser could find the classes inside that DLL.
I tried recompiling several different Console Applications as Class Libraries and got the same result. I also noticed that it works if I initially create the solution as a Class Library, but not if I convert it to one later.
Any tips?
You do not need to build it as a dll. VS 2010 (and IIRC 2008) allow referencing exe assemblies. All you need is for they relevant types to be declared public - top-level classes defualt to internal if you don't add a specifier.
You can switch output type to Class library in project properties as well - then you will have an output as dll instead exe file
What I've always done (since this is what you do with C++ static libraries, which is what I normally use - though I think it has some advantages for C# too) is add the class library's project to the solution, then add a reference to it in the project (or projects) that uses it.
When you go to add a reference, the list of potential references includes items from the solution, so it should be fairly obvious what to do. You should then get intellisense for your library.
One advantage of doing things this way is that if you need to edit files in the library project, it's very straightforward because they are close to hand, and the project then gets rebuilt automatically when you compile the solution.
Make sure that the classes in your dll project are public.
At first, from the point of view of managed libraries it does not matter what kind of Output type is your managed library. I mean that you can successfully reference ConsoleApplication1.exe from ConsoleApplication2.exe project (so you have no reason to convert ConsoleApplication1.exe to ConsoleApplication1.dll).
At second, I've tried to reproduce your situation, but... without effect. My VS displays types/methods from ConsoleApplication1.dll. One reason I can suppose is that you have forgotten to set visibility modifier (public keyword) for your utility classes.