I tried to follow some examples but I must be missing something here...
I am creating a class application .NET Framework 4.
I downloaded a required dll from the internet which I need to reference in my class application.
Should I add it or reference it in my project and how exactly should I do that?
I intend to share the finished dll with others, therefore the external dll I downloaded should be part of my final release - how to I do that?
And how to I use this dll in my class?
Thanks
Case 1:
If you have downloaded the dlls from NuGet using your Visual Studio plugin then you do not need to worry about anything.
As it will automatically update the package.config in your project
It will add the reference for this dlls to your project
Now, at the time of code check-in you should not check-in the dlls files but the package.config file only.
Case 2:
If you have downloaded the dlls manually from other sources but not using Visual studio plugin then you will have to manually add the dll reference to your project.
And you should also share these dlls with the code repository. So, that other people do not need to search for it on some other sites.
Case 3:
You can upload these dlls to Nuget.org (if security is not a concern) and make it available. in that case this will be one time process and you can just update the package.config as in case 1:
If you want to import a dll to your project, you can refer to the following steps.
1.Click Project => Add Reference...
2.Click Browser => Add the dll your want => Click OK
3.Add the using statement and call the method that in this dll.
using System;
using testdll;
namespace import_dll
{
class Program
{
static void Main(string[] args)
{
// Class1 is the class in testdll.dll
Class1 class1 = new Class1();
// Text is the method defined in Class1
class1.Text();
Console.ReadKey();
}
}
}
Related
When I am trying to call a method of another project class file, I am getting the following error
I have 2 C# library projects inside same solution file.
Inside each project I have a class file.
I have added reference of one project inside other.
There are NO compile time or build errors.
During runtime, it is throwing the following exception (Image attached above)
My question is, how does one able to call a class method of one project from another method ?
Thanks in advance :)
Is your current project class file exist in the actual directory? The physical file might not be on the disk itself so it has this error message.
Simply put if your physical file exist in your directory, and if you wanna call that method in, be sure to put the namespace at the top and import that reference from your solution explorer.
Also, Do remember to include your method class as public.
Example Below:
public class GetUserDetails
{
//methods
}
First check the target framework and verify that the two C# library projects have the same framework.
Did you miss deploying or referencing any assembly (DatabaseUtil.dll) in the old version?
Check to make sure that the referenced assembly exists in your bin folder. If it does exist, check if it is 32-bit or 64-bit.
Go to References in Solution Explorer in Visual Studio. Select the assembly which is getting complained.
Set its Copy Local to true in its Properties page.
And I suggest you try the suggestions in this thread.
If the above methods are not successful, please provide the relevant code to reproduce the problem.
Daniel Zhang
I have a project contains a classLibrary and webapplication now what I want is to use this project in another project means I want to add this entire project to another project as dll so developer of newer project cant use, update and see my code they just add in their project and simply use them without knowing whats going on in these dll. I am new to this can someone help me on this. I just to reuse code of webform's code behind and classlibrary .cs files code class which are used and called in webform's code behind.
When you add a class library,
it compiles into a dll.
you can explore the file-system's BIN folder of your project,
and see the dll there after a project build.
What you want to do is take that dll,
and add it as a reference to another project.
Note: You may need to include a using [dll namespace] statement if you want to access
the dll objects / functions without explicitly writing it every time.
I created a mini sample,
here are some screen shots that should guide you through the process.
I'm using VS 2010, but it should be similar 2012 / 2013.
Iv'e downloaded a C# interval tree collection class class from here http://intervaltree.codeplex.com/SourceControl/list/changesets -> Right hand side -> Download.
However I can't open the whole project on my Microsoft Visual C# 2010 Express (that also runs C# XNA) because
Solution folders are not supported in this version of the application
Also I just want the class to use separately in my own seprate project.
I tried to copy the three important seeming files Interval.cs, IntervalNode.cs and IntervalTree.cs into my project but this generated the compile error
There are no importers which handle this file type
I've also tried to copy and paste the contents of the three files into my project, encapsulating them into there own namespace as well as there was a lot of code. I had to rearange some of the usings a little but have run into the problem that possibly it wants PowerCollections .dll and .pcb files as using Wintellect.PowerCollections; causes
The type or namespace name 'Wintellect' could not be found (are you missing a using directive or an assembly reference?)
I'm not sure how to continue or if I'm doing the right thing at all in how to get this class to work.
Add the library to your solution
Copy the IntervalTreeLib directory into your solution directory. Then, right-click your solution, and add existing project. Point it at IntervalTreeLib.csproj in IntervalTreeLib, and click Open. That should add the IntervalTreeLib project to your solution.
Add a reference to the library in your project
Then, in your project, add a reference to the IntervalTreeLib proejct:
- Right click the References folder, and Add Reference. Click the Projects tab, and select IntervalTreeLib.
Use the classes in your code
To use classes from the library in your source then, you need to either add:
using IntervalTreeLib;
void Foo() {
IntervalTree<int, int> tree = new ...
}
Or, refer to them by their full name:
IntervalTreeLib.IntervalTree<int, int> tree = new ...
Open the IntervalTreeLib.csproj file if you want to be able to open the project in it's entirety (or in your current solution add an existing project (you can right-click on the solution) and select the IntervalTreeLib.csproj). If you are trying to grab just the code file in your project, ensure you also grab the PowerCollections.dll file (I see it is in the same folder as the code files) or your code will not compile (as you have discovered). You'll need to add a reference to it and include the needed using statement at the top of the code files making use of this library (or use fully qualified name with the namespace).
using IntervalTreeLib;
or
var myObj = new IntervalTreeLib.[WhateverClass](...);
Also, make sure you read the license.txt file. You may need to include it if you are using the code. Give credit where it is due.
UPDATE:
If the test project is causing you problems, just open the library project. Ideally you could just open that and compile it, adding the output DLL files that are generated directly into your solution. This is ideal unless you are planning on changing the library source code itself.
Add the library to the references of the project you want to use it.
Since discussing that you are able to build Intervallib.dll, we will discuss about how you should the dll in your project.
Now in your proj, right click on the references part and add the dll intervallib.dll to your references. In your game.cs file, have the reference to the namespace as -- using IntervalTreeLib;
then you should actually copy the dll powercollections.dll to the bin directory of proj directory also.
you should copy this dll because there is an indirect link to the dll as it is used in IntervalTreeLib.dll
following these steps, I was able to execute this project.
I have a file named common.cs which contains commonly used functions like this
public int GetColumnIndexByNameTemplateField(GridViewRow row, string SearchColumnName)
{
int columnIndex = 0;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is TemplateField)
{
if (((TemplateField)cell.ContainingField).HeaderText.Equals(SearchColumnName))
{
break;
}
}
columnIndex++;
}
return columnIndex;
}
Now I want to make that common.cs file change into common.dll, I created another class library project in which I copied all the code from common.cs file, added necessary references and built it to generate dll file, now I pasted that dll file from project debug directory into the bin directory of my asp.net project which is already using common.cs but whenever I try to import that dll using.
[DllImport("common.dll")]
public static extern String NewString(String x);
where NewString is another function, I get this error.
Unable to find an entry point named 'NewString' in DLL 'common.dll'.
I have checked already posted questions on stackoverflow as well as other guides on internet that tell how to make and use a dll but I don't know why I am getting this error also most of the guides show dll written in C++ where as my dll is written in C#, they also tell to use __declspec(dllexport) which tells that function is ready to be exported or something like that but I can't use this in C# dll I suppose.
I think its a basic question but this is my first time creating dll in C# so please help me out.
Thanks.
Ahmed if you want to turn any project into a library (.dll), double click the 'properties' folder in the solution explorer. Click the drop-down for project type and select 'Class Library' then click build, rebuild solution to recompile into a .dll.
Note that you can create a new ASP.NET project in the same solution by right clicking the solution and clicking 'add project'. Or you can open a new instance of Visual Studio. Once you open the ASP.NET project right click on references and click add reference. Click the browse tab, navigate to the folder containing the .dll and add it. Don't forget that any classes defined in the .dll must be marked as public or they will not be visible in other assemblies. Also don't forget the using statements for the namespaces that your classes are defined in.
When you click the 'Run Button' (the green arrow) it will always run the project that is in bold in the solution explorer. If a .dll is bolded then you will get an error that you cannot run a class library but you must instead change it to console application. If it is a console application then it must have a static void Main() function defined so it knows where to start. You can set any project as the startup project by right clicking it in solution explorer and click 'set as startup project'.
If you wish to run a program in your solution, but do not want to set it as the start up program than you can right click, go to debug and run. Organizing like-projects in a single solution can make maintaining them much easier.
Hope this helps.
You don't need to use DllImport. Since common.dll is a managed assembly, you simply need to add it as a reference in the ASP.NET project that's trying to use NewString.
if your assembly is written in c#, there's no need for a dllimport. rather go for add references like so - msdn entry (go for browse)
Paste the dll in the Bin folder of the site and you can use this by "using " at the top of the class of file then you will get all the methods of dll.
What do I do if I just want to create a project which contains a bunch of library functions? In other words no Main method is required. It seemed to be compiling a minute ago and then I added another .cs file and now I am confronted with this error message.
Create a .NET Class Library project if you only want a library project. If this is a project that already exists, you can set the Project Output type to a DLL ("Class Library") instead of an Executable ("Windows Application"/"Console Application") in the project properties.
What type of project did you create? It sounds like you meant to create a class library but accidentally created an executable assembly. Ensure that you are in fact creating a class library assembly (i.e. ".dll" not ".exe").
If you aren't using Visual Studio and are compiling your code with csc.exe then make sure that you are specifying /target:library to compile your code into a library.
You want to make the project a Class Library type. I believe you can change the type of project in the project properties settings.
or you could use the tried-and-true empty main method
I have the solution. Really simple. You wrote the static void main with lower case. You should write it like this: static void Main()
This problem is occured when we deleted App.xaml file from our project after the required method written due to that please ensure that your App.xaml file is in correct format with respective namespace and references, if it is not, create it and add it in your project.