Z
Z
Zimaell2020-12-09 19:14:34
C++ / C#
Zimaell, 2020-12-09 19:14:34

How to set local variable in switch?

I make a simple design

public int Test(){
  string yx1, yx2, yx3;
    foreach(KeyValuePair<string, Cell> kv in Cells){
      Cell obj = kv.Value;
      int x = obj.x;
      int y = obj.y;
      for(int n = 0; n < 8; ++n){
        switch(n){
          case 0: yx3 = y+"-"+(x+1); break;
          case 1: yx3 = y+"-"+(x-1); break;
          ..........................................................................
          }
       Debug.Log(yx3);
..........................................................................

that is, I immediately set the local variables yx1, yx2, yx3 in the method, then I redefine them in the loop and switch, I try to display the result in the debug, but I get an error
error CS0165: Use of unassigned local variable 'yx3'

they say yx3 is not defined, what is the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-12-09
@Zimaell

The fact that she lives only inside the switch. And for some reason you are pulling it outside its aisles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question