Hello everyone,
Sorry for this stupid question, as I am migrated from C++ to C#. In C++ there is no concept of property, except in COM.
For static property of a class in C# (e.g. DateTime.Now), I think there is nothing special compared to a non-static property, except we need to access the property by class name other than by instance variable name?
In my usage of static property, I feel no special point compared with static function. Correct?
thanks in advance,
George
George GeorgePosted Jun 13, 2008, 5:20 AM
Thanks man!
Question answered. Have a good weekend!
regards,
George
Jan MontanoPosted Jun 13, 2008, 5:15 AM
George GeorgePosted Jun 13, 2008, 2:36 AM
Cool, Jan!
I have tested each time counter will increase.Good sample.My further questions,
1. static property is just like two static methods (get/set), which could only access static members?
2. static property could access only static members, but non-static property could access both static and non-static members.
Both of them are correct?
regards,
George
Jan MontanoPosted Jun 13, 2008, 2:12 AM
Take a look at this example using static property. Try to debug Test1()
namespace WindowsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Test1();
}
private void Test1()
{
StaticProperty sp1 = new StaticProperty();
StaticProperty.StaticIncrementCount(); //check sp1.count & sp2.count
StaticProperty sp2 = new StaticProperty();
StaticProperty.StaticIncrementCount(); //check sp1.count & sp2.count
sp1.IncrementCount(); //check sp1.count & sp2.count
StaticProperty.StaticIncrementCount(); //check sp1.count & sp2.count
sp2.IncrementCount(); //check sp1.count & sp2.count
}
}
class StaticProperty
{
private static int count = 0;
public static int Count
{
get { return count; }
//set { count = value; }
}
public static int StaticIncrementCount()
{
return ++count;
}
public int ReturnCount()
{
return count;
}
public int IncrementCount()
{
return ++count;
}
}
}