English 中文(简体)
A. 造成某类问题
原标题:Problem creating instance of a class

我在2010年视觉演播室上设立了以下课程:

public class Bat : Form1
    {
        public int BatLocation;

        public void draw()
        {
            Pen batPen = new Pen(Color.Black);
            batPen.Width = 10;
            playArea.DrawRectangle(batPen, BatLocation, (picPlayArea.Height - 30), 50, 10);
        }
    }

但是,当我试图为这几类人树立榜样时,我却遇到一种 st然的过度现象,我建议我确保我不会有无限的 lo或无限的再次入侵。 我试图以以下两种不同方式创建这个案例:

Bat bottomBat;
bottomBat = new Bat();

以及

Bat bottomBat = new Bat();

But both ways return the same error when I try to run the program. I ve also tried the class definition with 以及 without the public modifier.

I m pretty new to programming 以及 have no idea what might be causing this problem. Am I doing something wrong?

<><>Edit>: <代码> Bat 类别是我目前拥有的一切,为它创造了一个特定建筑。 我认为我是否需要吗?

无论如何,这里是表格1的整类:

public partial class Form1 : Form
    {
        // Define various objects for the game
        public Graphics playArea;
        Bat bottomBat = new Bat();


        public Form1()
        {
            InitializeComponent();

            // Create instances of objects
            playArea = picPlayArea.CreateGraphics();
            //bottomBat = new Bat();

            // Delegate the mouseMove event for picPlayArea
            picPlayArea.MouseMove += new MouseEventH以及ler(picPlayArea_MouseMove);


        }

        private void picPlayArea_MouseMove(object sender, MouseEventArgs e)
        {
            bottomBat.Location = e.X;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            string msg = "Are you sure you want to exit?",
                   title = "Confirm Exit";

            DialogResult res = MessageBox.Show(msg, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res == DialogResult.Yes)
            {
                Environment.Exit(0);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // This is where most of the functionality is executed within the game
            playArea.Clear(Color.White);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
    }
最佳回答

似乎你把继承和组成结合起来,不可能。 基准<代码>Form1 类型中有宣布为衍生代码的外地。 Bat 。 此外,它利用一个外地初始者将其启动至new这一类。 显然,你有一个棘手问题:当你创建<条码>时。 Bat (or a Form1 for this matter), the field- originalizer willî- this will establish an instance of another Bat, which in another will establish yet anotherBat, and so on, ad-infinitum in theory. (在实践中:直至你离开平方位)。

这里有一个简单的固定办法,可以解决中转流问题,但可能不是大地设计的最合适办法:

public class Bat 
{
   public void Draw(Graphics playArea)
   {
       ...
   }
}

通知这种类别不再属于次类别:Form1;它直接继承。 系统。 目标。 现在没有<代码>Form1 Bat各班在设立时将进行有限的再入侵。

它不了解这里的最终目的,就难以建议“best> /em> 固定。 我建议你想到设计这些班级的最佳方法。 我认为,你需要花一些时间学习C#方案规划语言、业务厅的设计以及温和森林的具体内容。 页: 1 http://msdn.microsoft.com/en-us/library/system.windows.forms. Control.onpaint.aspx” rel=“nofollow”OnPaint

问题回答

原因往往混淆了财产及其背书变量。

大致如下:

public class tmp 
{ 
    private int _x; 

    ...

    public int X(x)
    {
        X = x;
    }

你有一个简单的问题。

页 次 巴特人来自表1,在表1中,你创建了一个新的巴特人案例,而表1则以表格1为基础,从而形成一种新的Bat.......,因此,在您的ack空间被使用之前,再重复。

一般而言,表1可能不应知道巴特语,而所有需要了解巴特语的法典都应属于巴特语。 然而,在特殊情况下,你可以解决这个问题:

partial class Form1
{
  public Form1(Bat _bat)
  {
    mBat = _Bat;
  }

  protected Bat mBat;
}

和Bat类

public class Bat : Form1
{
  public Bat() :  base(this)
  {

  }
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签