0

I am creating a web user control using C# to play some .wav files in the server. Following is my code.

public partial class WaveFilePlayer : System.Web.UI.UserControl
{
    //private string[] files;
    private static string[] files;

    protected void ButtonLoad_Click(object sender, EventArgs e)
    {
        string resourcePath = ConfigurationManager.AppSettings["ResourcePath"];
        string searchPattern = ConfigurationManager.AppSettings["SearchPattern"];

        files = System.IO.Directory.GetFiles(path, searchPattern);
    }

    protected void ButtonPlay_Click(object sender, EventArgs e)
    {
        int selectedIndex = ListBoxFiles.SelectedIndex;

        SoundPlayer soundPlayer = new SoundPlayer(files[selectedIndex]);
        soundPlayer.Play();
    }

As seen in the code above I declare string[] files as a member variable. Then I assign it in the ButtonLoad_Click method. But it throws a NullReferenceException when I try to access it in the ButtonPlay_Click method unless string[] files is declared static.

Does it mean that a new object of System.Web.UI.UserControl is not created while loading the user control in a asp.net page? And does it mean that when multiple clients (browsers) try to play the .wav files, only one instance of the string[] files is created at the server to be used by all clients?

manas
  • 397
  • 6
  • 25
  • 1
    What do you mean by "it is impossible to access it"? What error are you getting? – Jon Skeet Oct 02 '12 at 06:25
  • @JonSkeet it throws a `NullReferenceException` when accessing `files` variable – manas Oct 02 '12 at 06:28
  • Well, I suspect it's throwing a NullReferenceException when you *dereference* the value of the `files` variable. Just accessing it is fine, and will get you a `null` reference, for the reasons in my answer. Please be more specific in future questions - "impossible to access" sounds like a compile-time error... – Jon Skeet Oct 02 '12 at 06:31
  • @JonSkeet thanks, I'll try to be more specific in future to my best.. – manas Oct 02 '12 at 06:36

1 Answers1

3

I suspect the problem isn't that it can't be accessed, at all. I strongly suspect that the problem is that you've got two different requests, and therefore two different instances of WaveFilePlayer - so when you're executing ButtonPlay_Click, the files variable will be null unless you've written some code to persist it between requests.

Options here would be view state or some server-side session. You should not use a static variable, as that will basically mean you've got a single variable shared between all requests (for all users).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @manas: The second paragraph of my answer gives the alternatives. Basically you've got to propagate the data somehow - either via the client or at the server. This is a fundamental part of writing web applications. – Jon Skeet Oct 02 '12 at 06:32
  • Sorry, but it is a bit difficult for me to understand the first part of the second paragraph – manas Oct 02 '12 at 06:33
  • 1
    @manas: Which bit? View state? Did you try searching for "asp.net view state"? There's plenty of information on it. Likewise a search for "asp.net session" gives plenty of hits. – Jon Skeet Oct 02 '12 at 06:37