Hey guys, I have a million questions but the main one I need ahelp with is that I have a method in a class that plays sound and I am trying to call that method in the mainscreen class. So I create a new instance of the class that contains the makenoise method but when I create a new instance and use the method it does not work.

Also if I leave the programme to run for a bit it tells me an out of memory error, I spent hours last night trying to solve these errors but I'm at a dead end, can anyone here make any suggestions please?

Here is some code that I am using I dont want to paste it all just the parts I have written that are supposed to function:

class Alien: Enemy
    {
        public SoundPlayer aliensound = new SoundPlayer(Properties.Resources.scream2);


  public override void MakeNoise()
        {
            aliensound.Play();
            
        }



    public partial class MainScreen : Form
    {
        Alien alien;
       
alien = new Alien(pictureBox1, GamePanel);


   alien.MakeNoise();//this code is used in the paintevent method after an alien is removed this sound should play for it dyiing

Many thanks for any input whatsoever!

Hello, thanks for your reply. The file itself works as I can crete the object and link to the reource file and play it in a class by calling the object.Play(); within the same class, but I have a makeNoise method containg the object.Play(); which I want to call in another class by creating an instance of the class containing the makeNoise method and then typing instancename.makeNoise();

It doesnt show any errors but it does not play the sound, I do not know if its possible to do this in C#, I am new to this and trying to figure things out.

Since I tried this I keep getting out of memory errors, I have no clue how to fix this, I've been on it for hours.

Thanks for your help.

If you are creating the object in the method, then it is likely that it is being disposed when the method terminates, possibly before the sound has a chance to play.
Try creating the object at a higher scope level and call the makeNoise method when you need to.

This article has been dead for over six months. Start a new discussion instead.