Property VS Variable ??

  • Thread starter Thread starter Bobby
  • Start date Start date
B

Bobby

Hi all...

What is the different between using property and variable?
Their all functionality is same (for store a value). I get this issue when I
want find the best way to passing a parameter between form (Windows
application).
And, what is the best way to passing value to other form?


Thanks all..
 
A property encapsulates a private variable. So the property itself controls
access to the variable and invoke business rules if needed.

Example:
int m_inning ;

If m_inning is public, it could be assigned any valid int value. But what if
you meant m_inning to hold only 1 thru 9?
The following property would enforce that business rule.

public int Inning()
{
set
{
if (value < 1 || value > 9) throw new InvalidArgumentException();
m_inning = value;
}
}

kevin aubuchon
 
Think of properties as an extension of a field (or variable). But
properties don't denote storage space like a field but have accessors that
allow you to execute code when their values or written or read. You can
also add modifiers to properties such as virtual, abstract, or override that
apply to the accessors of the property.

One other thing to remember is that the variable name used in a public field
in a class is the name that will appear in Intellisense. Personally...
seeing something like m_AccountNumber in intellisense drives me nuts.
 
C Addison Ritchie said:
One other thing to remember is that the variable name used in a public field
in a class is the name that will appear in Intellisense. Personally...
seeing something like m_AccountNumber in intellisense drives me nuts.

And would violate current Microsoft guidelines, for whatever that's worth.
 
Back
Top