Solved

object properties - get set - question

Posted on 2004-08-19
10
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

630 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