Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2008 > BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 Search this Thread Display Modes
  #1 (permalink)  
Old November 28th, 2008, 04:13 PM
Registered User
 
Join Date: Nov 2008
Location: , , .
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
  #2 (permalink)  
Old November 29th, 2008, 10:01 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
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
================================================== =========
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
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



All times are GMT -4. The time now is 08:31 AM.


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