类型的异常' System.StackOverflowException'被扔了。'

时间:2017-09-21 07:01:18

标签: c#

为什么这样?这是我的代码。 这是我的" Kwadrat"类

public class Kwadrat : FormView
{
    public int X {get;set;}
    public int Y {get;set;}
    public Kwadrat Parent { get; set; }

    public void pozycja()
    {

        X = Parent.X;
        Y = Parent.Y;
    }

}

在这行Visual Studio中显示错误

Kwadrat k1 = new Kwadrat();

这是FormView.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace ObjectMoving
{
public partial class FormView : Form
{
    enum Position
    {
        Left, Right, Up, Down
    };
    private int NUMBER_OF_SQUARES=20;
    private int SQUARE_SIZE=15;
    Point _start = new Point(30,30);
    private Position _objPosition;
    Kwadrat k1 = new Kwadrat();
    Kwadrat k2 = new Kwadrat();
    private Kwadrat k3 = new Kwadrat();
    List<Rectangle> Snake = new List<Rectangle>();
    Losowanie l1=new Losowanie();

    public FormView()
    {
        InitializeComponent();

        _objPosition = Position.Down;

        k1.X = 30;
        k1.Y = 60;

        k2.Parent = k1;
        k2.X = 30;
        k2.Y = 45;

        k3.Parent = k2;
        k3.X = 30;
        k3.Y = 30;
        l1.losowa();



    }

    private void FormView_Paint(object sender, PaintEventArgs e)
    {
        for (int i = 0; i <= NUMBER_OF_SQUARES; i++)
        {
            //rysowanie lini horyzontalnych
            e.Graphics.DrawLine(new Pen(Brushes.Red),
                _start.X,
                _start.Y + SQUARE_SIZE * i,
                _start.X + SQUARE_SIZE * NUMBER_OF_SQUARES,
                _start.Y + SQUARE_SIZE * i);

            //Rysowanie lini wertykalnych
            e.Graphics.DrawLine(new Pen(Brushes.Brown),
                _start.X + SQUARE_SIZE * i,
                _start.Y,
                _start.X + SQUARE_SIZE * i,
                _start.Y + SQUARE_SIZE * NUMBER_OF_SQUARES);
        }


        e.Graphics.FillRectangle((Brushes.Aqua), k1.X, k1.Y, SQUARE_SIZE,        SQUARE_SIZE);
        e.Graphics.FillRectangle((Brushes.Aqua), k2.X, k2.Y, SQUARE_SIZE, SQUARE_SIZE);

        e.Graphics.FillRectangle((Brushes.Aqua), k3.X, k3.Y, SQUARE_SIZE, SQUARE_SIZE);

        e.Graphics.FillRectangle((Brushes.Red),l1.RandomNumber, l1.RandomNumber2, SQUARE_SIZE, SQUARE_SIZE);

        //foreach (Rectangle Object in Snake)
        //{
        //    e.Graphics.FillRectangle(new SolidBrush(Color.Aqua), k3.X, k3.Y, SQUARE_SIZE, SQUARE_SIZE);
        //}




    }


    //odswiezanie
    public void tmrMoving_Tick(object sender, EventArgs e)
    {
        //kolizja();
        k3.pozycja();

        k2.pozycja();




        if (_objPosition == Position.Right)
        {



            k1.X += 15;



        }
        else if (_objPosition == Position.Left)
        {

            k1.X -= 15;
        }
        else if (_objPosition == Position.Up)
        {

            k1.Y -= 15;
        }

        else if (_objPosition == Position.Down)
        {

            k1.Y += 15;
        }
        if (k1.X < 29)
        {
            tmrMoving.Enabled = false;
            tmrMoving.Stop();
            MessageBox.Show("Wyjechales poza plansze");
            Application.Exit();

        }
        else if (k1.X > 315)
        {
            tmrMoving.Enabled = false;
            tmrMoving.Stop();
            MessageBox.Show("Wyjechales poza plansze");
            Application.Exit();

        }
        else if(k1.Y<29)
        {
            tmrMoving.Enabled = false;
            tmrMoving.Stop();
            MessageBox.Show("Wyjechales poza plansze");
            Application.Exit();

        }
        else if (k1.Y > 315)
        {
            tmrMoving.Enabled = false;
            tmrMoving.Stop();
            MessageBox.Show("Wyjechales poza plansze");
            Application.Exit();

        }

        Invalidate();
    }
    //ruch strzalkami
    private void FormView_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left)
        {
           // s1.Sprawdzenie();           

            _objPosition = Position.Left;

        }
        else if (e.KeyCode == Keys.Right)
        {
            _objPosition = Position.Right;



        }
        else if (e.KeyCode == Keys.Up)
        {
            _objPosition = Position.Up;
        }
        else if (e.KeyCode == Keys.Down)
        {
            _objPosition = Position.Down;
        }
    }

    //public void kolizja()
    //{

    //    if (k1.X == l1.RandomNumber && k1.Y == l1.RandomNumber2)
    //    {
    //        l1.losowa();

    //        Snake.Add(new Rectangle(k3.X, k3.Y, 15, 15));

    //    }
    //}







}

}

1 个答案:

答案 0 :(得分:7)

你在这里遇到的问题是“循环参考”的味道。

您的1920 * 1080 (2) 800 * 600 (0) ... db.getCollection('testcollection').aggregate([ {$match: { $and:[ {"attr": {$elemMatch: {key: "network", value: {$in: ["4G","lan","wi-fi"]}}}}, {"attr": {$elemMatch: {key: "resolution", value: {$in: ["1920*1080"]}}}}, ]}}, { "$unwind": "$attr" }, { "$unwind": "$attr.value" }, { "$project": {_id:null,key:"$attr.key", value:"$attr.value", count:{$sum:1}} }, { "$group": {_id:{key:"$key",value:"$value"}, count:{$sum:"$count"}}}, ]) 课程基于Kwadrat,这意味着每当您创建FormView的新实例时,您调出的行Kwadrat构造,这意味着发生以下情况:

  1. 我们正在创建k1的新实例,因此请调用其构造函数
  2. 它继承自Kwadrat,所以我们调用它的构造函数并设置它的成员变量等。
  3. 我们在FormView,需要FormView的实例,名为K1,让我们创建
  4. 现在我们循环回“1”然后永远重复,这就是你收到Kwadrat的原因。在这个例子中,我们正在查看的类包含一个名为StackOverflowException的属性,它本身就是一个“红色鲱鱼”,因为它不是错误的原因,尽管如果你最终得到了Parent的一个实例Kwadrat设置为self(例如Parent)如果尝试遍历该实例的父级层次结构,则可能会看到相同的错误。< / p>