C#: Using a referenced DLL
I've created a DLL and referenced it to my project, but I cannot figure out how to actually use it. It appears that to make it work, I have to use some code like
MyClass class = new MyClass;
But I still can't get it to work. Here's my code:
using MyClass;
namespace NoName
{
public partial class Form1 : Form
{
publi开发者_高级运维c Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyClass MyClass = new MyClass();
Bitmap bmp = new Bitmap(MainImage.Image);
}
}
}
Make sure you have referenced the namespace that holds the types you want to use from your class library in the dependent program.
using <Namespace of MyClass>;
public static void Main()
{
MyClass blah = new MyClass();
}
In most cases the root namespace you are looking for should match the name of your referenced dll. e.g. DLLName.xxx ... where "DLLName" would be your root namespace and the anything after the period would signify child folders in a continuing hierarchy.
If you don't wan to add a reference to your class namespace (by means of the using reserved word) then you can completely qualified your class when you create an instance of it:
private void Form1_Load(object sender, EventArgs e)
{
MyClassNameSpace.MyClass MyClass = new MyClassNameSpace.MyClass();
Bitmap bmp = new Bitmap(MainImage.Image);
}
Actually when you create your instance of your class your variable name needs to not be the same case as your class.
MyClass myClass = new MyClass();
now you can use
myClass.MyMethod(x, y)
In your code posting your were using
MyClass MyClass = new MyClass();
If this is not the actual problem then give us an example of how you want to use MyClass (what methods or properties does it have?)
You should have using MyClassNamespace;
where MyClassNamespace
is the namespace of MyClass
.
精彩评论