Wrox Programmer Forums
|
BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
This is the forum to discuss the Wrox book Beginning Microsoft Visual C# 2008 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780470191354
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 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 November 28th, 2008, 04:13 PM
Registered User
 
Join Date: Nov 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using foreach when an Arraylist field is declared

I am trying to model a field boundary which is a polygon made up of lines which are defined by their end points which are x,y coordinates.
I am all right writing objects for points and lines but when I try to model the polygon as an 'ArrayList' of lines I can't use 'foreach' to access it. I want to use 'ArrayList as I don't know how many sides the polygon will have.
Quote:
quote:foreach statement cannot operate on variables of type 'RoutePlanner.Polygon' because 'RoutePlanner.Polygon' does not contain a public definition for 'GetEnumerator'
Code:
    public class Polygon 
    {
        private ArrayList lines; // A polygon is an array of Lines
        public Polygon(int sides) // Constructor. Parameter is number of lines
        {
            lines = new ArrayList(sides);
        }
        public Polygon()
        {
            lines = new ArrayList();// Constructor. null arraylist
        }
        public void Add(Line newLine)
        {
            lines.Add(newLine);
        }
    }

Code:
        private void btnTest_Click(object sender, EventArgs e)
        {
            Polygon myBoundary = new Polygon();
            Point MyPoint1 = new Point(2, 4);
            Point MyPoint2 = new Point(6, 8);
            Line MyLine1 = new Line(MyPoint1, MyPoint2);
            myBoundary.Add(MyLine1);
            Point MyPoint3 = new Point(12, 14);
            Point MyPoint4 = new Point(16, 18);
            Line MyLine2 = new Line(MyPoint3, MyPoint4);
            myBoundary.Add(MyLine2);
            foreach (Line myLine in myBoundary)
            {
                string MyString = string.Format("From Line x1= {0}  y1= {1}  x2= {2}  y2= {3}", 
                                        myLine.From.X, myLine.From.Y, myLine.To.X, myLine.To.Y);
                MessageBox.Show(MyString);
            }
        }
        

Regards
Colin
 
Old November 29th, 2008, 10:01 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm.

Why not do something like:
Code:
Line MyLine = null;
for(int i=0;i<myBoundary.Count;i++)
{
  MyLine = (Line)myBoundary[i];
  //Addtional logic
}
hth.
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Name 'CAM' is not declared. belmin45 ASP.NET 1.0 and 1.1 Basics 2 May 27th, 2008 02:13 AM
foreach with two lists silasla C# 13 May 2nd, 2008 12:27 PM
ForEach Loop Bushido121 BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 0 October 23rd, 2006 07:55 PM
"foreach" error BradBrening Pro PHP 2 January 13th, 2005 05:33 PM
Need help with foreach loop Arsi C# 3 September 7th, 2004 09:41 PM





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