Reputation: 2537
This is my code:
if (RdoBtnBeepDefault.Checked) SystemSounds.Beep.Play();
else SoundPlayer iPlay = new SoundPlayer(@TxtBeepFile.Text);
iPlay.Play();
And here's the error:
Embedded statement cannot be a declaration or labeled statement
If that isn't possible, mind telling me how?
Upvotes: 6
Views: 18912
Reputation: 5274
Not only that even this can produce the same error
if (RdoBtnBeepDefault.Checked) SystemSounds.Beep.Play();
else int i=0;
The reason is logic. If you put a single line statement in if else condition which indirectly means the conditional flow is ending with that line. In that case if you use some declaration||something as above which won't make sense/impact in any way, then that means It is kind of string literal. It is not exactly wrong, rather it is unneccessary. Mind c# visual studio editor reduces almost all the possible errors and unneccessary memory loading.
When you put braces, it indiactes that you may use that variable within the same block for some logic. So the editor will allow you to do that. At that time the VS assumes you may add the code in future. So it will give you only warning about that line. Without braces it is solidly assuming that you are not going to use that variable(due to scope).So, it takes that as error.
Upvotes: 3
Reputation: 263743
iPlay.Play();
is beyond the scope of your else
clause in your if-else
statement. Try enclosing it with braces for multiple line scope.
if (RdoBtnBeepDefault.Checked)
{
SystemSounds.Beep.Play();
)
else
{
SoundPlayer iPlay = new SoundPlayer(TxtBeepFile.Text);
iPlay.Play();
)
Upvotes: 9