Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Newbie question on using classes
Message
De
10/12/2009 11:41:37
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01438397
Message ID:
01438422
Vues:
51
This message has been marked as a message which has helped to the initial question of the thread.
>>>
>>>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 MethodName()
>>{
>>}
>>}
>>
>>
>>// then call like this
>>MyClassName.MethodName()
>>
>
>Thank you, Gregory. This explains what I was missing. It looks like having methods marked as static makes it easier to use them. Is there much downside to it, as far as you know?

Static methods can only use static methods and static fields - unless an object is passed as a parameter


It depends .

In a Person's class, a BirthDate would be an instance property - since it's different for each person

The method to calculate the age would not be static, since it will access the BirthDate of the instance

Sample
public class Person
  public DateTime BirthDate;

  public int CalcAge()
  {
     // use BirthDate, ie this.BirthDate
  }
}
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform