Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Newbie question on using classes
Message
 
 
À
10/12/2009 17:49:23
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01438397
Message ID:
01438518
Vues:
49
Bonnie,

Thank you for clarifying the point about static property. I actually have not thought about a static property as a way to hold the same value in all instances of a class. But after you mentioned it I remembered that Clipper had this feature which I really missed when switched to VFP.

To what I was trying to do, only God knows <g> (just kidding). I am working with Kevin Goff's book on VS 2005 Reporting and trying to learn "his" way of creating strongly typed datasets. He is using these dataset for Crystal Reports but I plan to use them in data entry forms as data source of controls. So I was creating some test methods to see what they return and so on. Therefore, the point of making certain methods static was simply for convenience of calling them.

>Dmitry,
>
>It might be a good idea if you could tell us what you plan to do with this class. The reason I say that is because, since you're new to .NET, you may or may not "get" the point of using static methods/properties.
>
>As Gregory's example with the Person class pointed out, you wouldn't want to use Birthdate as a static property because that would be different in every instance of the Person class. If it wasn't obvious from his explanation, I just wanted to clarify that when you use static properties they will contain exactly the same values in every instance of the class. If you have several instances of the class, and in one instance you decide to make a change to a static property, then all the other instances of that class will automagically have that new value. If this is the kind of thing you want, then all is good.
>
>Now, I know your specific question was about a static method, not property, but depending on what your method does it may need to use static properties ... so it may still be relevant to your question
>
>~~Bonnie
>
>
>>>>>>
>>>>>>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
>>>  }
>>>}
>>>
>>
>>Thank you for the explanation.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform