Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Basic functionality C# question

Avatar of mpoots
mpoots asked on
C#
2 Comments1 Solution479 ViewsLast Modified:
Hi,

I have just started work with C# after working many years with Delphi. I am trying to write a simple program to get the feel of C#. In my program I want to draw a Tangram Puzzle. I have a Base class called Shape and classes that inherit from them called Square and Triangle. The base class Shape holds an array of points. When I create a triangle and a Square both will have the same points. Why is that?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TangramWorld;
using Shapes;
 
namespace Tangram
{
    public partial class frmMain : Form
    {
 
        private TangramPuzzle _TangramPuzzle;
 
        public TangramPuzzle Tangram
        {
            get{
                if (_TangramPuzzle == null)
                    _TangramPuzzle = new TangramPuzzle(pbMain);
                return _TangramPuzzle;
            }
        }
        public frmMain()
        {
            InitializeComponent();
        }
 
        private void btnInitialize_Click(object sender, EventArgs e)
        {
            Tangram.Initialize();
        }
    }
}
 
using System;
using Shapes;
using System.Drawing;
using System.Windows.Forms;
 
namespace TangramWorld
{
    public class TangramPuzzle
    {
        private System.Windows.Forms.PictureBox _PictureBox;
        private Boolean _Initialized;
        public static Shape[] ShapeArray;
 
        public TangramPuzzle(System.Windows.Forms.PictureBox aPictureBox)
        {
            _PictureBox = aPictureBox;
        }
 
        public void Paint()
        {
            if (_Initialized)
            {
                /*
                foreach (Shape Shp in ShapeArray)
                {
                    if (Shp != null)
                    {
                        Shp.Paint();
                    }
                }*/
 
                for (int i = 0; i <2; i++)
                {
                    if (ShapeArray[i] != null)
                    {
                        ShapeArray[i].Paint();
                        //MessageBox.Show("First");
                        break;
                    }
                }
            }
            else 
                Initialize();
 
		}
 
        public void Initialize()
        {
            ShapeArray    = new Shape[2];
            
			ShapeArray[0]             = new Triangle(new Point(100,100),new Point(200,200),new Point(100,300));
			ShapeArray[0]._PictureBox = this._PictureBox;
			ShapeArray[0].DrawPen     = new System.Drawing.Pen(Color.Blue, 2);
 
			ShapeArray[1]             = new Square(new Point(210, 100), new Point(310, 100), new Point(310, 200), new Point(210, 200));
			ShapeArray[1]._PictureBox = this._PictureBox;
			ShapeArray[1].DrawPen     = new System.Drawing.Pen(Color.Green, 2);
           
            _Initialized = true;
            Paint();
        }
    }
}
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
 
/// <summary>
/// Summary description for Class1
/// </summary>
namespace Shapes
{
    public class Shape
    {
        private System.Drawing.Graphics g;
        public  System.Drawing.Pen DrawPen  = new System.Drawing.Pen(Color.Red, 2);
        private System.Drawing.Pen ClearPen = new System.Drawing.Pen(SystemColors.Control, 3); // To make a shape invisiable
 
        protected static Point[] Points;
        public System.Windows.Forms.PictureBox _PictureBox;
        public GraphicsPath path = null;
        public Color BackColor = Color.Aqua;
        public Color ForeColor = Color.White;
 
        public Shape()
        {
            // TODO: Add constructor logic here
        }
 
        public void Paint()
        {
            g = _PictureBox.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.DrawPolygon(ClearPen, Points);
            g.DrawPolygon(DrawPen, Points);
        }
    }
 
    public class Triangle : Shape
    {
        public Triangle(Point aPoint1, Point aPoint2, Point aPoint3)
        {
            Points = new Point[4];
            Points[0] = aPoint1;
            Points[1] = aPoint2;
            Points[2] = aPoint3;
            Points[3] = aPoint1; // Back to startpoint 1
        }
    }
 
    public class Square : Shape
    {
        public Square(Point aPoint1, Point aPoint2, Point aPoint3, Point aPoint4)
        {
            Points = new Point[5];
            Points[0] = aPoint1;
            Points[1] = aPoint2;
            Points[2] = aPoint3;
            Points[3] = aPoint4;
            Points[4] = aPoint1; // Back to startpoint 1
        }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of ASliborsky
ASliborsky

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers