简体   繁体   中英

Use my own .cs classes in a Visual Studio project

I'm sure this question is rather stupid...

I have a Visual Studio 2010 project (Project1). In this project I have a Form (FormPrincipal) and a class (ClassAux). I want to use methods of ClassAux in the code of FormPrincipal.

I tried with:

   using ClassAux;

and also:

   using Project1.ClassAux; 

but when I try to use one method (ClassAux has a getInstance() method using Singleton pattern) is says "The name does not exist in the current context".

I also checked and both classes (form and class) use the same namespace.

How should I do?

Thank you

Looks you're mixind up different ideas, keyword using is meant for getting access to types from namespaces. To access methods of class X, either create an object of this class or make the method static and acess it directly via classname.

Since you are using VS 2010, I would recommend using the features of your IDE. If both files are part of your project, it can help create the using.

You said you have a getInstance() method. If this is public and static, then type the following in a method in your form's .cs file.

var instance = ClassAux.getInstance();

Then put your cursor on the word ClassAux, and click Ctrl + . A couple options will appear in the Smart Tag under the name. The first one will offer to create the correct using at the top of the file.

The using statement at the top only provides a method to shorten the type names in the file, and it can't contain the name of a class, only the namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM