Search code examples
c#error-handlingmedia-player

Creating A Soundboard In C# .NET Dim Statement Doesnt Turn Blue


I'm quite new to c# but most of the times I can handle the errors being thrown my way but this one just doesn't seem to work. Not sure if I'm too close to the issue or something.

-Problem-

I've been working on a small project called "SoundBoard For Kids" where I want to create an educational software like a Sound book, nothing too fancy just something to get my knowledge going, and I've made it pretty far into the code but now when I try to make a button play a sound I cant get the code to work, The "Dim" statement doesnt want to light up, neither does the "As String". Am I forgetting to add something? I'm too afraid to add a Media Player because i'm worried it will mess up the whole code.

        private void button1_Click(object sender, EventArgs e)
    {
        Dim sPath As String
        Dim mySound As Media.SoundPlayer

        sPath = "Location of where I have the sound file.wmv"
        mySound = new Media.SoundPlayer(sPath)
        mySOund.Play()
    }

Solution

  • Since you have not provided error code I will assume it is syntax error.

    Your button_click event should look something close to this:

    private void button1_Click(object sender, EventArgs e)
    {
        string sPath; //Declaration pattern: Variable Type followed by Name
        System.Media.SoundPlayer mySound; //if you're not using namespace full path
    
        sPath = "Location of where I have the sound file.wmv";
        mySound = new Media.SoundPlayer(sPath);
        mySOund.Play();
    }
    

    Since dim is used in VB and does not exist in C#. You should declare variables as shown above. Following pattern: type, name and optional - provide variables with values.

    Note: reference types should be assigned before using, otherwise it will throw NullReferenceException.