Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Newbie question on using classes
Message
De
10/12/2009 11:20:03
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01438397
Message ID:
01438404
Vues:
60
This message has been marked as a message which has helped to the initial question of the thread.
>Hi,
>
>I created a simple project with one Windows form. Then I added a class (Class1.cs) to this project. In the Class1.cs I created a couple of methods (one method is public).
>
>Then I try to call a method of the class in Class1.cs from Click method of the form. In the Form1.cs I added
>using myClass1NameSpace;
>
>But when I enter (in the form button Click() method) something like MyClassName. the intellisense does not show the public method in the class MyClassName. And when I enter this method anyway, I get error "An object reference is required for the nonstatic field, method, or property".
>
>What am I missing?
>
>TIA.


Dmitry,

If the method of the class is not marked as static, you have to instantiate the class prior to calling the method
var obj = new MyClassName();
obj.MethodName();
If the method of the class has been marked as static, you can call it without instantiating the class
// class definition
public class MyClassName
{

public static void MethodName()
{
}
}


// then call like this
MyClassName.MethodName()
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform