nfung
August 6th, 2003, 01:25 AM
Two simple questions:
1. Why "__property"? What does it offer that regular "method" does not?
2. When instantiating managed class..
SomeClass MyInstance; (illegal, if "SomeClass" is a managed class.
SomeClass* pInstance = new SomeClass (legal)
Why did they design it as such? Is there any purpose behind this?
Here's a short code to illustrate the point:
__gc class Dummer
{
protected:
int m_nValue;
//Keep track of how many times a propert has been accessed.
int nCountRead;
int nCountWrite;
public:
Dummer() { nCountRead=0; nCountWrite=0; }
//Property - public. But what's the use exactly? How's this different from a member function?
__property int get_Val()
{
nCountRead++;
return m_nValue;
}
__property int set_Val(int nVal)
{
nCountWrite++;
m_nValue=nVal;
return 1;
}
};
// This is the entry point for this application
int _tmain(void)
{
// TODO: Please replace the sample code below with your own.
Console::WriteLine(S"Hello World");
//QUESTION 1: What's the purpose of "__property"?? What's the difference between "__propery" and "method"??
Dummer * Tom = new Dummer;
Console::WriteLine(Tom->get_Val());
//QUESTION 2: Error C3149 here. You must declared John as a "pointer" - but why did the .NET team assert that all managed class instance must be "pointer" to the class at hand??
/*
Dummer John;
John.m_nValue = 10;
*/
return 0;
}
1. Why "__property"? What does it offer that regular "method" does not?
2. When instantiating managed class..
SomeClass MyInstance; (illegal, if "SomeClass" is a managed class.
SomeClass* pInstance = new SomeClass (legal)
Why did they design it as such? Is there any purpose behind this?
Here's a short code to illustrate the point:
__gc class Dummer
{
protected:
int m_nValue;
//Keep track of how many times a propert has been accessed.
int nCountRead;
int nCountWrite;
public:
Dummer() { nCountRead=0; nCountWrite=0; }
//Property - public. But what's the use exactly? How's this different from a member function?
__property int get_Val()
{
nCountRead++;
return m_nValue;
}
__property int set_Val(int nVal)
{
nCountWrite++;
m_nValue=nVal;
return 1;
}
};
// This is the entry point for this application
int _tmain(void)
{
// TODO: Please replace the sample code below with your own.
Console::WriteLine(S"Hello World");
//QUESTION 1: What's the purpose of "__property"?? What's the difference between "__propery" and "method"??
Dummer * Tom = new Dummer;
Console::WriteLine(Tom->get_Val());
//QUESTION 2: Error C3149 here. You must declared John as a "pointer" - but why did the .NET team assert that all managed class instance must be "pointer" to the class at hand??
/*
Dummer John;
John.m_nValue = 10;
*/
return 0;
}