'How do I call the injected dependency 'level' and use it as a condition in an IF statement? C#
An object reference is required for the non-static field, method, or property 'Form1.level'
public Form1( int level ) {
this.level = level;
playtune();
InitializeComponent();
}
// the value is actually 1 and the 0 is just to declare the variable
int level = 0;
static void playtune()
{
SoundPlayer tune = new SoundPlayer(
GameProject.Properties.Resources.level_audio_mario);
if (level == 2)
{
tune = new SoundPlayer(GameProject.Properties.Resources.level_audio_sonic);
}
tune.Play();
}
Solution 1:[1]
Can you change your method to non static?
static void playtune()
=>
void playtune()
{
SoundPlayer tune = new SoundPlayer(GameProject.Properties.Resources.level_audio_mario);
if (this.level == 2)
{
tune = new SoundPlayer(GameProject.Properties.Resources.level_audio_sonic);
}
tune.Play();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Markus Meyer |
