473,786 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1488
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**********@n ospammeexcite.c om> wrote in message
news:um******** ******@TK2MSFTN GP02.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**********@n ospammeexcite.c om> wrote in message
news:um******** ******@TK2MSFTN GP02.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**********@n ospammeexcite.c om> дÈëÓʼþ
news:um******** ******@TK2MSFTN GP02.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
1976
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 control it? Yossi.
8
1465
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 "document.all.style is not an object" for whatever layer I have created first. It seems as though when the second layer is created, it overwrites the first one ....
18
18419
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 a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
5
1745
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 exists: foo.Height = (my loaded value)... I am trying to load and set the property on the fly - without necessarily knowing what it is. Imagine a 100 name/value pairs in a file (config, res, txt, whatever). I want to load and set those on the fly,...
8
2267
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 happens. dim filt as string ... build filter string... UserControl.ReportFilter = filt
4
6252
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):"+ dt1.ToUniversalTime().ToString()); Doing the following returns 1/1/1970 5:00:00 AM
7
2009
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
11082
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 how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
8
16097
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 which modifies Application setting section but how to add new item to userSettings ? Andrus.
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.