Reputation: 107
i'm trying to learn C# with graphics, using sdl.net at the moment. 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();
}
}
Upvotes: 3
Views: 87
Reputation: 58980
In your ball
class, you have:
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.
Upvotes: 2
Reputation: 18142
Your issue is that you're using a static variable:
private static int numeris;
Change it to be an instance:
private int numeris;
Upvotes: 3