Solved

object properties - get set - question

Posted on 2004-08-19
10
286 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please explain purpose of GZIP 4 83
Converting PHP triple des decrypt to c# 9 172
Load XML element 3 43
web api controller with multiple posts that accept different objects 1 25
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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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