We help IT Professionals succeed at work.

Adding a Class Reference to a C# Windows Forms

tsmolskow
tsmolskow asked
on
Medium Priority
243 Views
Last Modified: 2010-04-16
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.
Comment
Watch Question

Ravi SinghSenior Software Engineer

Commented:
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.
Senior Software Engineer
Commented:
Sorry, don't call your object reference "object" like I did in my example, its a class in the framework!

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.

o
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.