简体   繁体   中英

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. 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.

Is there a way to declare a variable as 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:

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. So nothing will change if you'll remove try-catch completely here

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

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.

So when you declare local variable within try-catch block, it can be referred only within try-catch block. 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 .

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.

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;

var x = y;

this will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM