Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2006, 01:51 PM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you all for your great, informative, answers.

Please don't be offended, but how can I check if a property has been set?

Are you thinking about comparing the property's current value to its default value and, if they are different, conclude that it was set? If yes, ok - I understand -, but it is more of a "change check" than a "it was used" check.
 
Old May 2nd, 2006, 02:29 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

No offense taken. After all, this is the Beginner's forum, isn't it?
And even in another forum you can easily ask these kind of questions.

There are at least two ways to do it:

1. With a default value:

private int xCoord = -1;

Then in the set accessor of a property called XCoord

set
{
  if (xCoord == -1)
  {
     xCoord = value;
  }
}

This is a bit risky, because -1 could be a legal value as well. In .NET, nullable types were introduced so you could make it a nullable variables as well.

2. Keep a separate variable for tracking:

private int xCoord = -1;
private bool xCoordHasBeenSet = false;

Then in the set accessor of a property called XCoord

set
{
  if (!xCoordHasBeenSet)
  {
     xCoordHasBeenSet = true;
     xCoord = value;
  }
}

This uses the internal xCoordHasBeenSet variable to determine whether the value has been set before. If it has been set, xCoordHasBeenSet becomes true and you can no longer set it.

I am not sure if users of your control will understand such a mechanism though. It feels a bit awkward to have a get/set property that can only be set once, although obviously it will do what you need...

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old May 2nd, 2006, 05:29 PM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the replies! I will now try to apply these teachings to my project. It is sooo easy to guess that I will unveil a ton of new problems :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Server Control....Custom Property Editor ZArrinPour ASP.NET 1.0 and 1.1 Basics 1 June 15th, 2010 11:30 AM
Web Service, Custom Control, Custom Return Type robzyc ASP.NET 2.0 Basics 6 June 10th, 2008 08:03 AM
custom control inside custom control issues StevesonD ASP.NET 2.0 Professional 1 February 19th, 2008 06:54 PM
multiple constructors for custom controls three BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 0 May 1st, 2006 03:24 PM
Multiple Constructors Page 194 NUTSHELL BOOK: Beginning Java 2 2 February 5th, 2004 11:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.