Error with built-in operations

This is my code:

if (RdoBtnBeepDefault.Checked) SystemSounds.Beep.Play(); else SoundPlayer iPlay = new SoundPlayer(@TxtBeepFile.Text); iPlay.Play(); 

And here is the error:

Embedded statement cannot be a declaration or labeled statement

If this is not possible, think about how?

+6
source share
2 answers

iPlay.Play(); goes beyond your else clause in your if-else . Try to include it with brackets for several areas.

 if (RdoBtnBeepDefault.Checked) { SystemSounds.Beep.Play(); ) else { SoundPlayer iPlay = new SoundPlayer(TxtBeepFile.Text); iPlay.Play(); ) 
+9
source

Not only this can lead to 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 an else condition, which indirectly means that the conditional stream ends with this line. In this case, if you use any declaration || something, as indicated above, which does not make sense / influence in any way, which means that it is a string literal. This is not entirely true, but it is not. Mind C # visual studio editor reduces almost all possible errors and unnecessary memory usage.

When you set curly brackets, it indicates that you can use this variable in one block for some logic. Therefore, the editor will allow you to do this. At that time, VS suggests that you can add code in the future. So this will only give you a warning about this line. Without curly braces, he firmly assumes that you are not going to use this variable (due to scope). So she accepts this as a mistake.

+3
source

Source: https://habr.com/ru/post/925623/


All Articles