Solved

object properties - get set - question

Posted on 2004-08-19
10
283 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
 

Author Comment

by:sethUSer420
ID: 11850921
AdrianJMartin- how can you use an internal variable for data binding?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

16 Experts available now in Live!

Get 1:1 Help Now