• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Adding a Class Reference to a C# Windows Forms

I have created a class in a seperate file and I want to use that class in my WindowsForm file, how can I reference the class in my WindowsForm file.
  • 2
1 Solution
Ravi SinghSenior Software EngineerCommented:
Hi, assuming your using Visual Studio .NET and the class is compiled into a DLL then you have to reference the DLL file by going to Project --> Add Reference --> Browse. You then have to refer to the class in your code by specifying its full namespace, e.g.:

ClassNameSpace.ClassName object = new ClassNameSpace.ClassName();

or you could add a using directive at the top of the file to include the namespace, then you can simply refer to the class using its name.

If its just a .cs file then you can add that file into your project by going into Project --> Add Existing Item and again, use the same method above to refer to it in your code.
Ravi SinghSenior Software EngineerCommented:
Sorry, don't call your object reference "object" like I did in my example, its a class in the framework!
tsmolskowAuthor Commented:
It is a cs file, and I have added it to the project from the solution, but I'm not sure what to do next, can you give me some code example.
once you have declared the instance you can use the methods with dot notation so.....

ClassName obj = new ClassName();

obj.MethodName(Arg1, Arg2);

for methods returning values....

string str = obj.MethodName(Arg1, Arg2);

hope this helps.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now