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?