enter image description here

It is necessary that the nickname be displayed along with the message, but in this situation it is not visible

Closed due to the fact that off-topic party PashaPash 16 Oct '16 at 9:59 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Please remember to paste the code, not the screenshot of the code. - Alex

2 answers 2

This is a variable from another method, you can’t get it. Create a variable in the class and access it already

class Test { public int i; public void Test(int a){ i = a; } public void Test2(){ int b; b = i; } } 

    Variables declared inside a code block have a scope limited to this block. Those. outside the block, access to such a variable is impossible. Blocks of code, for example, is a method, the body of a loop or a conditional operator, or just a pair of curly braces.

    For example, such code will not compile, because in the same block we set two variables with the same name:

     void foo() { bool a = true; bool a = false; // ошибка! повторное объявление переменной в том же блоке кода } 

    and here so it, because we carried apart announcements on different blocks

     void foo() { // блок кода метода // здесь переменная a будет недоступна { // блок1 bool a = true; } // здесь переменная a будет недоступна {// блок 2 bool a = false; } // здесь переменная a будет недоступна } 

    Thus, in your case, you need to increase the scope of the variable nickname so that it becomes visible in all the necessary methods. The easiest way to do this is to declare a variable a member of the class to which these methods belong.