Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 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 June 24th, 2008, 09:01 AM
Authorized User
 
Join Date: Sep 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question about the Name Value Class used BugBase

I am new to OO and .NET in general. I want to be sure that I am using best practices when possible. I want to use the NameValue class that you describe in the BugBase chapter. In that chapter there is a line of code that populates the Bug business object from the database (theBug.Status = new NameValue(myReader...)).

I was wondering why the Bug classes constructor didn't have a line like: this.status = new NameValue();?

My thought was that if you new'ed the status variable in the Bugs constructor that you wouldn't need 'new' the status in multiple places (my webpages create new instances of the Bug class when I insert of update a bug so I have to 'new' the status in the code that loads the bug business class in my webpage and in the code that populates the bug class from the database).

Before I make this change (minor as it is), I want to be sure that there is not a specific reason why putting this.status = new NameValue(); in the Bugs constructor is a bad idea.

I know that this is a minor somewhat picky question. I am not asking it because I disagree with the way you implemented this. Just the opposite. I have found that your comments are right on the money. I just want to be sure that I am not making a mistake.

Thanks

 
Old June 24th, 2008, 04:39 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I am not sure what you asking or suggesting. The Bug class *does* instantiate new NameVaulue objects in its Private Variables region:

...
Private _frequency As NameValue = New NameValue
Private _status As NameValue = New NameValue
Private _createdDateAndTime As DateTime = DateTime.MinValue
...

So, whenever you New up a Bug, fields like _status already contain a valid NameValue instance and you can directly set the Name and Value properties if you want to.

When I load the data, I do this:

theBug.Status = New NameValue(myReader.GetInt32(myReader.GetOrdinal("S tatusId")), _
     myReader.GetString(myReader.GetOrdinal("StatusDesc ription")))

I effectively overwrite the existing Status NameValue instance. IMO, this is easier to read and code than setting individual fields on the Status instance (which would work fine as the Bug already has valid NameValue instances:

theBug.Status.Value = myReader.GetInt32(myReader.GetOrdinal("StatusId"))
theBug.Status.Name = myReader.GetString(myReader.GetOrdinal("StatusDesc ription"))

Both code examples would result in the same Status instance.

Does that help?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 25th, 2008, 06:43 AM
Authorized User
 
Join Date: Sep 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes this does help. Thanks so much. I am embarrassed to say that I wasted your time (sorry). I missed the line of code:

    Private _status As NameValue = New NameValue

I mistakenly only looked at the constructor to see if the _status variable was instantiated. Is it a better practice to initialize member variables the way you did in the Bug class then to initialize them in the constructor? If so, why?

Thanks. I really appreciate the guidance

 
Old June 25th, 2008, 05:03 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Both would work in most circumstances.

I usually initialize simple properties like Ints, Strings and simple classes like NameValue directly within the private fields region. More complex types, like Collections that need some state are usually placed in a constructor.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old June 26th, 2008, 06:42 AM
Authorized User
 
Join Date: Sep 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have to say that your help has made me a better programmer.

Thanks

 
Old June 26th, 2008, 07:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Glad to hear that. That's the whole point about Wrox books.... ;)

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about abstract class nf0001384 C# 2005 5 July 22nd, 2013 06:58 AM
Question about value class type saadli BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 4 December 3rd, 2008 10:15 PM
Formatting Question in the BugBase mikener BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 5 June 6th, 2008 01:45 PM
question about sub New() in class hertendreef ASP.NET 2.0 Basics 7 January 3rd, 2007 02:10 PM
BugBase: .CCS question rjd BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 November 21st, 2006 05:21 PM





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