473,387 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Setting Properties (correction)

(on the prior message the code snippet was wrong. this is the one that
executes the set in a loop until a stack overflow occurrs)
(using 1.1)
I have the following property defined in an ascx control.

public string Title //property
{
get {return this.Title; }
set {this.Title = value; }
}

Whan code on a page that contains this control sets the control, for
instance
myControl.title = "this is a heading"
The set executes over and over until a stack overflow occurs.

I am just learning C# and I know there must be something wrong but I don't
have much docs on property setting.

Thanks,
T
May 18 '06 #1
3 1471
Hi Tina,

A stack overflow exception occurs when there is an infinite loop. In your
case, you have a property called "Title" that has a get and a set method.
Both methods refer back to the property itself, so it infinitely recurses,
calling it's own get and/or set method whenever it is referenced. Instead,
it should probably refer to a private or protected field, as in:

private string _Title;
public string Title
{
get { return _Title; }
set { _Title = value; }
}

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Complex things are made up of
lots of simple things.

"Tina" <ti**********@nospammeexcite.com> wrote in message
news:um**************@TK2MSFTNGP02.phx.gbl...
(on the prior message the code snippet was wrong. this is the one that
executes the set in a loop until a stack overflow occurrs)
(using 1.1)
I have the following property defined in an ascx control.

public string Title //property
{
get {return this.Title; }
set {this.Title = value; }
}

Whan code on a page that contains this control sets the control, for
instance
myControl.title = "this is a heading"
The set executes over and over until a stack overflow occurs.

I am just learning C# and I know there must be something wrong but I don't
have much docs on property setting.

Thanks,
T

May 18 '06 #2
You're causing recursion by setting the Title property from within the Title
property "setter" and getting the Title property from within the Title
property "getter". You'll need to store the Title information, for example,
in a field.

private string _title;

public string Title //property
{
get { return _title; }
set { _title = value; }
}

--
Tim Wilson
..NET Compact Framework MVP

"Tina" <ti**********@nospammeexcite.com> wrote in message
news:um**************@TK2MSFTNGP02.phx.gbl...
(on the prior message the code snippet was wrong. this is the one that
executes the set in a loop until a stack overflow occurrs)
(using 1.1)
I have the following property defined in an ascx control.

public string Title //property
{
get {return this.Title; }
set {this.Title = value; }
}

Whan code on a page that contains this control sets the control, for
instance
myControl.title = "this is a heading"
The set executes over and over until a stack overflow occurs.

I am just learning C# and I know there must be something wrong but I don't
have much docs on property setting.

Thanks,
T

May 18 '06 #3
You need define a private attribute title to hold the value internally, and
change code to following:

private string title;

public string Title //property
{
get {return this.title; }
set {this.title = value; }
}

Pls note inside get and set, use title, not Title.
Otherwise it will cause a endless loop.

Ryan

"Tina" <ti**********@nospammeexcite.com> дÈëÓʼþ
news:um**************@TK2MSFTNGP02.phx.gbl...
(on the prior message the code snippet was wrong. this is the one that
executes the set in a loop until a stack overflow occurrs)
(using 1.1)
I have the following property defined in an ascx control.

public string Title //property
{
get {return this.Title; }
set {this.Title = value; }
}

Whan code on a page that contains this control sets the control, for
instance
myControl.title = "this is a heading"
The set executes over and over until a stack overflow occurs.

I am just learning C# and I know there must be something wrong but I don't
have much docs on property setting.

Thanks,
T

May 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Yossi | last post by:
I want to set the asp session id path property. how can I do that? I mean, asp session id is stored in a cookie and like any other cookie it should have properties (or attributes), how can I...
8
by: Eclectic | last post by:
I have a couple of layers that are hidden. When an image is moused over, I want to show the appropriate layer, then hide it on mouseOut. The problem is, I get an error telling me...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
5
by: Brad | last post by:
All samples related to this see to come short of being 'truly' dynamic. For instance, after creating all the code to load/save a properties value, you turn around and save it to one you KNOW...
8
by: David Lozzi | last post by:
Howdy, I have a user control that is a report to display data. On the page the control is inserted in, I have filter options to filter the report. When I try to do something like this, nothing...
4
by: Maziar Aflatoun | last post by:
Hi, Can someone please tell me how I can set DateTime to 01/01/1970 (UTC). Doing the following DateTime dt1 = new DateTime(1970, 1,1); Debug.WriteLine("dt1(utc):"+...
7
by: Ronald S. Cook | last post by:
In a .NET Windows app, if I set somehting like the title of the form to "MyApp" at run-time, will that make the app run slightly slower than if I had set the title at design-time? Thanks, Ron
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
8
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.