简体   繁体   English

添加到列表中的不同对象变得相同

[英]Different objects added to the list become same

i'm trying to learn C# with graphics, using sdl.net at the moment. 我正在尝试用图形学习C#,目前使用sdl.net。 I wanted to learn how to create objects on screen, but stuck on this crazy thing for three days. 我想学习如何在屏幕上创建对象,但坚持这个疯狂的事情三天。 Googled everything i could think of, but can't solve this on my own and i'm asking somebody for help. 谷歌搜索我能想到的一切,但不能自己解决这个问题,我要求别人帮忙。

I simplified the example to bare minimum. 我简化了这个例子。 For the moment i try to create a few objects by clicking mouse on screen and print their numbers. 目前我尝试通过在屏幕上单击鼠标并打印他们的数字来创建一些对象。 On creation, every object gets a number, i put it on the list and then foreach every object. 在创建时,每个对象都得到一个数字,我将它放在列表中,然后预先处理每个对象。

But when i print the object list, every object becomes like the last one, though constructor shows that it creates new object with new number. 但是当我打印对象列表时,每个对象都变得像最后一个,尽管构造函数显示它创建了具有新数字的新对象。 But when i iterate the list, every object has same number. 但是当我迭代列表时,每个对象都有相同的数字。

Constructor: object number:1
Show() object number:1
object count:1

Constructor: object number:2
Show() object number:2
Show() object number:2
object count:2

Constructor: object number:3
Show() object number:3
Show() object number:3
Show() object number:3
object count:3

When it should be: 它应该是:

Constructor: object number:3
    Show() object number:1
    Show() object number:2
    Show() object number:3
    object count:3

What am i doing wrong, what am i missing?? 我做错了什么,我错过了什么? (i tried same principle with windows forms and buttons, and it worked fine) (我尝试了与窗体和按钮相同的原理,它工作正常)

My simplified code: 我的简化代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using SdlDotNet.Graphics;
using SdlDotNet.Input;
using SdlDotNet.Core;
using Font = SdlDotNet.Graphics.Font;

public class KeyboardTest
{
    private static Surface m_VideoScreen;
    private static Surface m_DrawingSurface;

    private static List<ball> ball_list=new List<ball>();//### THE OBJECT LIST ###
    private static int num = 1;

    class ball  //### MY CLASS ###
    {
        private static int numeris;

        public ball(int _numeris) //### CONSTRUCTOR ###
        {
            numeris = _numeris;
            System.Diagnostics.Debug.WriteLine("Constructor: object number:{0}", numeris);
        }

        public void show() //### VOID TO PRINT OBJECT NUMBER ###
        {
            System.Diagnostics.Debug.WriteLine("Show() object number:{0}", numeris);
        }
    }

    public static void Main(string[] args) // ### MAIN VOID, SKIP THIS ###
    {
        m_VideoScreen = Video.SetVideoMode(800, 600, 32, false, false, false, true, true);
        m_VideoScreen.Fill(Color.White);
        m_DrawingSurface = Video.CreateRgbSurface(m_VideoScreen.Width, m_VideoScreen.Height, 32, 0, 0, 0, 0, true);
        m_DrawingSurface.Fill(Color.White);  
        Events.Quit += new EventHandler<QuitEventArgs>(ApplicationQuitEventHandler);
        Events.Tick += new EventHandler<TickEventArgs>(ApplicationTickEventHandler);
        Events.TargetFps = 1;
        Events.MouseButtonDown += new EventHandler<MouseButtonEventArgs>(ApplicationMouseButtonEventHandler);
        Events.Run();
    }

    private static void ApplicationMouseButtonEventHandler(object sender, MouseButtonEventArgs args)
    {
        if (args.Button == MouseButton.PrimaryButton && args.ButtonPressed==true)
        {
            ball_list.Add(new ball(num)); //### ADDING TO THE LIST ON MOUSE CLICK ###
            num += 1;
        }
    }

    private static void ApplicationTickEventHandler(object sender, TickEventArgs args)
    {
        m_VideoScreen.Blit(m_DrawingSurface);
        foreach (ball temp in ball_list) //### PRINTING NUMBERS OF ALL OBJECTS IN LIST ###
        {
            temp.show();
        }
        m_VideoScreen.Update();
        System.Diagnostics.Debug.WriteLine("object count:{0}", ball_list.Count);
    }

    private static void ApplicationQuitEventHandler(object sender, QuitEventArgs args)
    {
        Events.QuitApplication();
    }
}

Your issue is that you're using a static variable: 您的问题是您使用的是静态变量:

private static int numeris;

Change it to be an instance: 将其更改为实例:

private int numeris;

In your ball class, you have: private static int numeris; 在你的ball ,你有: private static int numeris;

A static variable is one that's at the class level, not at the instance level. 静态变量是在级别而不是在实例级别的变量。 Your ball instances all are sharing that number. 你的球实例都在分享这个数字。

从Ball类中删除numeris中的Static关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ListBox,实现相同接口的不同对象的列表 - ListBox, list of different objects implementing the same interface 对象继续添加到列表中 - Objects continue to be added to the List LINQ to Objects:如何在同一个对象列表中连接不同的属性 - LINQ to Objects: How to join different properties in the same List of objects 如何防止同一类型表单的对象同时添加到列表中 - How can you prevent objects of the same type form being added to a list at the same time 未使用EFcore添加到数据库中的对象列表 - List of objects not added to the database with EFcore 将不同的字符串添加到同一列表中的对象的正确方法是什么? - What is the proper way to add different strings to objects in same list? 如何将另一行添加到 bindingsource ? &#39;添加到 BindingSource 列表的对象必须都是相同的类型。 - How to add another row to a bindingsource ? 'Objects added to a BindingSource's list must all be of the same type.' 不同对象的相同 GetHashCode() - Same GetHashCode() for different objects 每个添加的 object 的 Unity List 似乎是相同的项目,但它们应该不同......? - Unity List every object added appears to be the same item when they should be different…? 通过OpenTK未呈现的其他线程添加到Dictionary的对象 - Objects added to Dictionary by different thread not rendered by OpenTK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM