sethUSer420
asked on
object properties - get set - question
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
AdrianJMartin- how can you use an internal variable for data binding?
ooops just public and protected, you can't use internal because the .aspx is contained within a separate dynamically created assembly.
> my friend eternal, may have just added some more brackets then needed by mistake.
I sure did... but they cancel out right??? ;)
I sure did... but they cancel out right??? ;)
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.
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.
ASKER
how can you use an internal variable for data binding?
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.
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.
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.