Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 9th, 2007, 04:06 PM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Larryz
Default iterator..... s

i am new this whole thing.... and its quiet hard for me
to get to grips with it....

The following code is from a book , so hardly my own work...

Although the code for calculating prime numbers is a bit tricky
to follow, i don't think thats the purpose of the code... as such

What i find hard to get is the way it works... i mean..... there is
no explicit call... to GetEnumerator()

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace c11ex03
{
  public class primes
    {
      private long min;
      private long max;

// what is the purpose of the default constructor
// on the line below...
// is it used since the non default constructor
//is what sets up the min and max values.
//i dont know why the code uses ... this(2, 100)
//when the primes are between 2 and 1000, it seems like a typo.
// but when new to coding i could be wrong.
// and in anycase this constructor has no body...
// default constructor seem pointless....what can a function or //constuctor with no body be of any use.. in fact i am sure
// that c# can create them by default... what ever that means.
//quoting for visual c#: "When you define a class in C#, there is
// no need to to "define" assocciated constructors and destructors
//because the base class System.Object provides default implementation
// for you... so when do you and when dont you..
// as mentioned in any case the default one is bypassed.. or is it.
      public primes(): this(2, 100)
      {
      }
      public primes(long minium, long maximum)
      {
          if (min < 2)
              min = 2;
          min = minium;
          max = maximum;
      }
      public IEnumerator GetEnumerator()
      {
          for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
          {
              bool isPrime = true;
              for (long possibleFactor = 2; possibleFactor <=
                  (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
              {
                  long remainderAfterDivision = possiblePrime % possibleFactor;
                  if (remainderAfterDivision == 0)
                  {
                      isPrime = false;
                      break;
                  }
              }
              if (isPrime)
                  yield return possiblePrime;
          }

      }
  }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace c11ex03
{
    class Program
    {
        static void Main(string[] args)
        {
            primes primesFrom2To1000 = new primes(2, 1000);
            foreach (long i in primesFrom2To1000)
                Console.Write("{0} ", i);
            Console.ReadKey();
        }
    }
}



 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Arrays using JavaScript,/JSP Iterator in IE celticbhoy Javascript How-To 3 December 11th, 2007 09:55 AM
Iterator Scope Problem smokey_gun Javascript How-To 1 July 4th, 2007 01:26 AM
XML Traversing using Navigator and Iterator classe jasim XML 1 July 18th, 2003 03:29 AM



All times are GMT -4. The time now is 11:58 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.