Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Newbie question on using classes
Message
 
 
To
10/12/2009 11:41:37
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01438397
Message ID:
01438424
Views:
38
>>>>
>>>>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform