Click here to Skip to main content
14,923,034 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;
             case "East":
                 heading = currentRoom.ConnectEast;
             case "South":
                 heading = currentRoom.ConnectSouth;
             case "West":
                 heading = currentRoom.ConnectWest;
         return heading;

As always, I appreciate the help.

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.
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.
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.
enum Increment{ Down = -1, None = 0, Up = 1, };
struct Direction {
   public Increment Vertical, Horizontal;

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:
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't_repeat_yourself[^].

Learn anti-patterns well and hit your own hand when it reaches for a work described as an anti-pattern:[^].

lw@zi 25-May-11 0:28am
You may have scared OP. :) Have a five.
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?
Sergey Alexandrovich Kryukov 25-May-11 15:38pm
And yes, it looks you were right. Useless.
Please see the re-post of OP:

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

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