如何解决这个共同的范围问题 [英] How to solve this common scope problem

查看:69
本文介绍了如何解决这个共同的范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常遇到这个问题,而我要做的就是返回我的交换机正在操作的东西.但是,这是一个常见的问题,其中我有一个本地字符串,将其传递到try/catch或if语句中,并且返回值未将其确认为已使用的字符串".错误是使用未分配的变量".

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.

推荐答案

这是因为,如果不执行任何case块,标题仍可以取消分配.如果在默认情况下将其设置为某些值,它将起作用.
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.


整个想法真的很糟糕.无论使用哪种语言或平台,这种风格都会破坏整个编程思想.

您会看到,您使用了立即数常量"North","West"……想象一下您输错了一个.您如何支持它?我可以告诉你:如果要继续这样,最好完全放弃编程.尝试看看编写好的代码.

让我们看看.
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;
}

//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:

void Move(double step, DirectionOnChart direction) {
    CurrentCoordinate.X = step * (int) direction.EastWest;
    CurrectCoordinate.Y = step * (int) direction.NorthSouth;
}



这样的事情.简单,不是手动代码,没有临时代码,没有立即常量(它们属于资源,输入文件,至少一个显式常量块).
切勿重复一段代码或数据. 不要重复自己(DRY),请参见 http://en.wikipedia.org /wiki/Don''t_repeat_yourself [ ^ ].

很好地学习反模式并在达到描述为反模式的工作的目的时自己动手: ^ ].

—SA



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


这篇关于如何解决这个共同的范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆