Link to home
Start Free TrialLog in
Avatar of sethUSer420
sethUSer420

asked on

object properties - get set - question

I want to make use of single value data binding in asp.net. Using page level properties seems like a good way.

I declare a page level property like:

public class page : System.Web.UI.Page
{
  protected int x = 0;
  protected int itemID
  {
    get {  return x; }
  }
  {
    set {  x = value; }
  }

  private void Page_Load(object sender, System.EventArgs e)
  {
    //blah
  }
}

my question is, can I use a property like a variable? set values to it and get values from it without going through another variable. Like:

  protected int itemID
  {
    get {  return itemID; }
  }
  {
    set {  itemID = value; }
  }

am I forced to use another variable to hold the values a property facilitates coming and going?

Seth
ASKER CERTIFIED SOLUTION
Avatar of eternal_21
eternal_21

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AdrianJMartin
AdrianJMartin

Its worth bearing in mind that Properties are not variables(fields), they are Methods, they provide a conveient way of transferring data between objects. There does not have to be a one to one , property to variable relationship. ie one property set method can alter as many variables as the scope allows....They can also be used to implement conversions, eg a string property can parse it value and set several variables, raise events on setting or getting , conditionally raise events on set and get .....etc

If you really want a Property to behave like a variable then use a public /protected  or internal variable!
You can use these for data binding too.
Avatar of sethUSer420

ASKER

AdrianJMartin- how can you use an internal variable for data binding?
ooops just public and protected, you can't use internal because the .aspx is contained within a separate dynamically created assembly.
> my friend eternal, may have just added some more brackets then needed by mistake.

I sure did... but they cancel out right??? ;)
sethUSer420

One thing that you may want to keep in mind... If you were to do what you suggest you would create an infinite loop!  I did this one time by accedent while refactoring some code... Here is the senario...

public int ItemId {
   get { return ItemId; }
   set { ItemId = value; }
}

So when I did

ItemId = 21;

The set tried to set ItemId, but that called the same property until I got a recursion error.
how can you use an internal variable for data binding?
The Learned One,

It seems like this question was answered by eternal_21 and then different aspects were discussed by  chintan_vaishya,  AdrianJMartin and myself (NipNFriar_Tuck).  I think that at the very least eternal_21 should be awarded some points.