Solved

object properties - get set - question

Posted on 2004-08-19
10
285 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
ID: 11849464
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
ID: 11849626
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
ID: 11850899
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:sethUSer420
ID: 11850921
AdrianJMartin- how can you use an internal variable for data binding?
0
 
LVL 5

Expert Comment

by:AdrianJMartin
ID: 11851053
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
ID: 11851860
> 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
ID: 11852288
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
ID: 11856328
how can you use an internal variable for data binding?
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12102651
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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