Wrox Programmer Forums
|
BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8
This is the forum to discuss the Wrox book Professional C# 2008 by Christian Nagel, Bill Evjen, Jay Glynn, Morgan Skinner, Karli Watson; ISBN: 9780470191378
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8 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 August 20th, 2009, 11:47 AM
Registered User
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Enumerator problem

Hello,

This question relates to chapter 5 in the book. I'm getting an error when building the HelloCollection code.

'HelloCollection.Enumerator' does not contain a
constructor that takes '0' arguments

Code:
public class HelloCollection : IEnumerable {
			public IEnumerator GetEnumerator() {
				Enumerator enumerator = new Enumerator();
				return enumerator;
			}
			public class Enumerator : IEnumerator, IDisposable {
				private int state;
				private object current;
				
				public Enumerator(int state) {
					this.state = state;
				}

Clearly, the Enumerator constructor here does require one argument, and clearly the Enumerator is instantiated without passing in an argument. The book shows it this way, and I could not find a code example for this in the downloads. (The download has the HelloCollection that comes first in the book, the second version of it appears to be missing)

Should I be instantiating the Enumerator using a value of say 0?
 
Old September 25th, 2009, 04:21 PM
Registered User
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I'm slowly making my way through the book. It'd be fantastic to know for sure if this is or is not an error in the book...

Also, the errata form has about 20 some fields to fill in. Come on, I'm not giving you my phone number (a required field ffs) just so that I can post an error in ***your*** book...

Here is possibly another error:

Page 814
string [] lines = new string[nLines];


Doesn't that need to use parenthesis instead of square brackets? new string(nLines);

Cheers
 
Old December 7th, 2009, 03:35 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Thumbs up

you can write your Enumerator class like this:

Code:
public class Enumerator : IEnumerator, IDisposable 
{
   private int state;
   private object current;

   public Enumerator(int state) 
   {
      this.state = state;
   }

   public Enumerator() :this(0)
   {}
}
because you don't have defined parameterless constructor in your class, when you try to create a new object of this class in 'Enumerator enumerator = new Enumerator();' command you get and exception.
 
Old December 7th, 2009, 05:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Quote:
Here is possibly another error:

Page 814
string [] lines = new string[nLines];


Doesn't that need to use parenthesis instead of square brackets? new string(nLines);
C# uses brackets.

Code:

// verbose
int[] a = newint[4];
for (int i = 0; i < a.Length; ++i)
{
   a[i] = (i + 1) * 2;
}
 
// compact
int[] b = newint[] { 2, 4, 6, 8 };
//ultra-compact
int[] c = { 2, 4, 6, 8 };
The Following User Says Thank You to Bob Bedell For This Useful Post:
irProject (December 18th, 2009)









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