[英]How can I declare an empty variable?
Here's my code: 这是我的代码:
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
{
}
}
I can't call game.Participants in my foreach loop because i initialize game inside the try statement. 我无法调用game.foreach循环中的参与者,因为我在try语句中初始化了游戏。 I can't initialize game outside the try statement either though because to do that i would have to give it a temporary value and I don't know what kind of value it will be. 我也不能在try语句之外初始化游戏,因为要做到这一点,我必须给它一个临时值,而我不知道它将是什么样的值。
Is there a way to declare a variable as null? 有没有办法将变量声明为null? Or is there potentially a different way to solve this? 还是有可能以其他方式解决此问题?
You should declare variable before try-catch
block, otherwise it will not be visible outside the try-catch
block: 你应该声明前变量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)
{
}
Note that your current try-catch
block don't catch anything except RiotSharpException
and even for that type of exceptions you simply rethrow it. 请注意,您当前的try-catch
块除了RiotSharpException
之外什么都不会捕获,即使对于这种类型的异常,您也可以将其重新抛出。 So nothing will change if you'll remove try-catch
completely here 因此,如果您在此处完全删除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)
// ...
Further reading: 3.7 Scopes from C# specification 进一步阅读: 3.7 C#规范的范围
The scope of a name is the region of program text within which it is possible to refer to the entity declared by the name without qualification of the name. 名称的范围是程序文本的区域,在其中可以引用名称声明的实体,而无需对该名称进行限定。 Scopes can be nested 范围可以嵌套
And especially 特别是
• The scope of a local variable declared in a local-variable-declaration (§8.5.1) is the block in which the declaration occurs. •在local-variable-declaration(第8.5.1节)中声明的局部变量的范围是声明所在的块。
So when you declare local variable within try-catch
block, it can be referred only within try-catch
block. 因此,当您在try-catch
块中声明局部变量时,只能在try-catch
块中引用它。 If you declare local variable within method body block, it can be referred within method body scope and within nested scopes. 如果在方法主体块中声明局部变量,则可以在方法主体范围内和嵌套范围内引用局部变量。
Something like this: 像这样:
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
}
}
Please, notice, that try {} catch (RiotSharpException ex) {throw;}
is a redundant construction and can be dropped . 请注意, try {} catch (RiotSharpException ex) {throw;}
是多余的结构,可以删除 。
Could you do something like this? 你能做这样的事情吗? I dont know what type your GetCurrentGame
retuns from the api so I just used GameType
as a placeholder. 我不知道您的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
{
}
}
尝试这样的事情:
var game = (Object)null;
string y = null; 字符串y = null;
var x = y; var x = y;
this will work 这会工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.