2017-06-06 96 views
0

这里是我的代码:如何声明一个空变量?

private void button1_Click(object sender, EventArgs e) 
    { 
     var api = RiotApi.GetInstance("KEY"); 


     try 
     { 
      var game = api.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188); 
     } 

     catch (RiotSharpException ex) 
     { 
      throw; 
     } 

     foreach (var player in game.Participants) // Can't find game variable 
     { 

     } 
    } 

我不能叫game.Participants在我的foreach循环,因为我try语句里面初始化游戏。我不能在try语句之外初始化游戏,但是因为要这样做,我必须给它一个临时值,我不知道它会是什么样的价值。

有没有办法将变量声明为null?或者是否有解决此问题的其他方法?

+0

像'游戏游戏= NULL;'?请注意,你不能放'var game = null;'并且必须声明实际的类型。 –

+1

考虑将循环移入尝试,因为如果游戏不存在或不存在,您不想访问游戏? –

+1

在try块内包含foreach有什么错误? –

回答

-1

尝试这样的:

var game = (Object)null; 
+0

游戏绝对不是字符串 –

+0

对不起,我编辑它... –

+0

这只是声明'game'为'Object',然后你将不能够做到'的foreach(在game.Participants VAR播放器)'。 –

6

之前try-catch块应声明变量,否则它不会是try-catch块外部可见:

TypeOfGame game = null; // declare local variable here 
// note that you should provide initial value as well 

try 
{ 
    // assigne it here 
    game = api.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188); 
} 
catch (RiotSharpException ex) 
{ 
    // I hope you have some real code here 
    throw; 
} 

// now you can use it 
foreach(var player in game.Participants) 
{ 

} 

注意,目前的try-catch块不要”除了RiotSharpException之外什么也没有,甚至对于那种类型的例外,你只需重新抛出它。所以,什么都不会改变,如果你删除try-catch这里完全

var api = RiotApi.GetInstance("KEY"); 
// if api can be null, then you can use null-propagation operation ?. 
var game = api?.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188); 
if (game == null) // consider to add null-check 
    return; 

foreach(var player in game.Participants) 
    // ... 

延伸阅读:3.7 Scopes从C#规范

一个名字的范围是程序文本的区域内,它是 可能引用名称声明的实体而不具有 名称的资格。范围可以嵌套

,尤其是

•在 局部变量声明中表示的局部变量的范围(第8.5.1节)是其中 声明所在的块。

所以当你声明try-catch块内的局部变量时,它只能在try-catch块内引用。如果在方法体块中声明局部变量,则可以在方法体范围内和嵌套范围内引用它。

+0

可能是一个想法,如果在foreach异常在他真正的代码(而不是仅仅投掷)处理 –

+0

@KlitosKyriacou同意,也想过这个问题,但目前的代码任何异常的情况下,之前做一个空检查,我们不会进入循环。我觉得您的评论是一个很好的说明脱颖而出OP来记住这一点 –

+0

难道不game.Participants在for循环翻倒如果赶上抛出? –

-2

string y = null;

var x = y;

这将工作

0

你能做这样的事吗?我不知道你的GetCurrentGame是什么类型的API回退,所以我只用GameType作为占位符。

private void button1_Click(object sender, EventArgs e) 
{ 
    var api = RiotApi.GetInstance("KEY"); 

    GameType game = new GameType();   

    try 
    { 
     game = api.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188); 
    } 

    catch (RiotSharpException ex) 
    { 
     throw; 
    } 

    if(game == null || !game.Participants.Any()) return; 

    foreach (var player in game.Participants) // Can't find game variable 
    { 

    } 
} 
+0

不幸的是,这并不作为占位符:(返回类型RiotSharp.CurrentgameEndpont.CurrentGame – FastCow

4

事情是这样的:

private void button1_Click(object sender, EventArgs e) 
{ 
    var api = RiotApi.GetInstance("KEY"); 

    // if we have api, try get the game 
    var game = api != null 
     ? api.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188) 
     : null; 

    // if we have game, process the players 
    if (game != null) 
     foreach (var player in game.Participants) 
     { 
      //TODO: put relevant logic here 
     } 
} 

请,通知,即try {} catch (RiotSharpException ex) {throw;}冗余建设和可下降

+0

为什么冗余 – FastCow

+3

@FastCow工作:你*抓*例外呢?并且什么都不做,但是*再次扔掉它 –