Solved

object properties - get set - question

Posted on 2004-08-19
10
282 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:sethUSer420
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 125 total points
Comment Utility
No, you cannot use a property like a variable in the sense you have described.  You must have an actual "variable" somewhere in your code if you want to retain data in between calls to a specific property.

e.g.:

  private int _itemId;
  protected int itemID
  {
    get {  return _itemID; }
  }
  {
    set {  _itemID = value; }
  }

The seconds snippet of code you posted will eventually result in a stack overflow (i.e. exception), as the propertly keeps calling the get method over and over (for calls to Get or Set).
0
 
LVL 3

Assisted Solution

by:chintan_vaishya
chintan_vaishya earned 125 total points
Comment Utility
You can use the property just like a Variable, but that property is not a Variable, only holds the variable, to which it can set the value or get value from.

like,
public class1
{
   private int itemID;
   public int ItemID
   {
     get { return itemID; }
     set { itemID = value; }
   }
}

value is a reserved word for facilitating this.

Now from some other class you can use this property just like a Variable,

LIke,

class1.ItemID = 6;

or

int tempInt = class1.ItemID;

Why would you need to avoid the variable, and use property streight away??
As here we are,

Declaring the variable as private, and so only methods in the class can access it.
By providing property for that, we are having more control on that.

By the way,
my friend eternal, may have just added some more brackets then needed by mistake.

Hope this is Useful.
Good Luck.
Chintan.


0
 
LVL 5

Expert Comment

by:AdrianJMartin
Comment Utility
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.
0
 

Author Comment

by:sethUSer420
Comment Utility
AdrianJMartin- how can you use an internal variable for data binding?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:AdrianJMartin
Comment Utility
ooops just public and protected, you can't use internal because the .aspx is contained within a separate dynamically created assembly.
0
 
LVL 10

Expert Comment

by:eternal_21
Comment Utility
> my friend eternal, may have just added some more brackets then needed by mistake.

I sure did... but they cancel out right??? ;)
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
Comment Utility
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.
0
 

Author Comment

by:sethUSer420
Comment Utility
how can you use an internal variable for data binding?
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now