0

I have a problem with my script, here it is:

string myOutput = "image.png"; if (myOutput.Contains("youtu.be")) { string statementOutput = "Video ouput"; } else { if (myOutput.Contains(".png")) { string statementOutput = "Image output"; } else { string statementOutput = "Nothing's here"; } } Label1.Text = statementOutput; 

With code above I get compile time error:

The name 'statementOutput' does not exist in the current context

What I wanna do is, if my string for example have "youtu.be" in the myOutput variable it makes the string "statementOutput" to the value "Video Ouput" and if it contains .png it changes the strings value to "Image Ouput" and if there is nothing in the "myOutput" string, it does nothing.

3 Answers 3

2

I think the problem is that you are declaring "statementOutput" in all the if-else statements. Try using

string myOutput = "image.png"; string statementOutput; if (myOutput.Contains("youtu.be")) { statementOutput = "Video output"; } else { if (myOutput.Contains(".png")) { statementOutput = "Image output"; } else { statementOutput = "Nothing's here"; } } Label1.Text = statementOutput; 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

string myOutput = "image.png"; string statementOutput = "Nothing's here"; if (myOutput.Contains("youtu.be")) { statementOutput = "Video ouput"; } else if (myOutput.Contains(".png")) { statementOutput = "Image output"; } Label1.Text = statementOutput; 

2 Comments

That is also wrong. Whoever up voted is as much in a hurry as you were
+1. Default value probably should be String.Empty... With else if way of writing conditions it should be easier to chain any more conditions as needed.
0

This should work:

string myOutput = "image.png"; string statementOutput = ""; if (myOutput.Contains("youtu.be")) { statementOutput = "Video ouput"; } else { if (myOutput.Contains(".png")) { statementOutput = "Image output"; } else { statementOutput = "Nothing's here"; } } Label1.Text = statementOutput; 

The problem is the scope of the variable. In case you're declaring your variable, you are declaring a variable that will exist only in current block of code, in which has been declared. In the way you wrote your code, in each of these code blocks different variable is declared, and at the end of each block, it is destroyed. You should clear yourself that even thought these variables have the same name, they are definitely not same.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.