Click here to Skip to main content
16,011,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I constantly run into this problem and all I want to do is return what my switch is operating on. And yet, this is a common problem where I have a local string, pass it into a try/catch or if statement and the return doesnt acknowledge it as a "used string". The error is, instead, "use of an un-assigned variable".

public Int32 MovePlayer(String direction)
     {
         Int32 heading;
         switch (direction)
         {
             case "North":
                 heading = currentRoom.ConnectNorth;
                 break;
             case "East":
                 heading = currentRoom.ConnectEast;
                 break;
             case "South":
                 heading = currentRoom.ConnectSouth;
                 break;
             case "West":
                 heading = currentRoom.ConnectWest;
                 break;
             default:
                 break;
         }
         return heading;
     }


As always, I appreciate the help.
Posted

That's because heading can still be unassigned if none of case blocks are executed. If you set it to something in the default case, it will work.
 
Share this answer
 
Comments
Isaiah83 24-May-11 23:32pm    
Thank you.
Sergey Alexandrovich Kryukov 24-May-11 23:48pm    
That is correct (my 5), but look at the code....
So, please look at my answer.
--SA
The whole idea is really bad. This style defeats the whole idea of programming, no matter what language or platform.

You see, you use immediate constants "North", "West"… Imagine you mistype one. How can you support it? I can tell you this: if you're going to continue like this, it's better to give up programming at all. Try to see how good code is written.

Let's see.
C#
enum Increment{ Down = -1, None = 0, Up = 1, };
struct Direction {
   public Increment Vertical, Horizontal;
}

//or
struct DirectionOnChart {
   public DirectionOnChart(Increment eastWest, Increment northSouth) {
      EastWest = eastWest;
      NorthSouth = northSouth;
   }
   public Increment EastWest, NorthSouth;
}


Now, you never need East, West, etc., never need switch.

Here how you move in some direction:
C#
void Move(double step, DirectionOnChart direction) {
    CurrentCoordinate.X = step * (int) direction.EastWest;
    CurrectCoordinate.Y = step * (int) direction.NorthSouth;
}


Something like that. Simple, not manual code, no ad-hoc code, no immediate constant (they belong in resources, input files, at least a block of explicit constants).
Never repeat a piece of code or data. Don't Repeat Yourself (D.R.Y.), see http://en.wikipedia.org/wiki/Don't_repeat_yourself[^].

Learn anti-patterns well and hit your own hand when it reaches for a work described as an anti-pattern: http://en.wikipedia.org/wiki/Anti-pattern[^].

—SA
 
Share this answer
 
v3
Comments
dan!sh 25-May-11 0:28am    
You may have scared OP. :) Have a five.
Sergey Alexandrovich Kryukov 25-May-11 0:43am    
Thank you very much.
About getting scared... Imagine you have a deadly seek patient. He can die or get badly disabled and suffer for the rest of his life. But you're a surgeon who offer a surgery. It also presents some risk of death, but... who needs such life? The patient may get scared but can you avoid it? Can you allow yourself not getting him a chance?
:-)
--SA
Sergey Alexandrovich Kryukov 25-May-11 15:38pm    
And yes, it looks you were right. Useless.
Please see the re-post of OP: http://www.codeproject.com/Questions/201828/Mechanics-of-Room-Navigation-through-Arrays.aspx.

I recommended to change OP's mind or leave the industry. Seriously.
Please see my answer.
--SA
sairam.bhat 25-May-11 2:19am    
My +5
Sergey Alexandrovich Kryukov 25-May-11 2:52am    
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900