Use of unassigned local variable. How to resolve?

Asked

Viewed 105 times

0

I’m developing a site that requests an api and shows the data to users, but I’m having problems:

Use of unassigned local variable 'value'

Use of unassigned local variable 'date'

To better understand:

inserir a descrição da imagem aqui

Page code with error:

public class IndexModel : PageModel
  {
    private readonly ILogger<IndexModel> _logger;
    public Global Global { get; private set; }
    public Country[] Countries { get; private set; }
    public DateTime Date { get; private set; }
    public IMemoryCache MemoryCache { get; private set; }
    public IndexModel(ILogger<IndexModel> logger, IMemoryCache memoryCache)
    {
      _logger = logger;
      MemoryCache = memoryCache;
    }

    public async Task OnGetAsync()
    {
      try
      {
        string key_countries = "countries_cache"; // Chave do cache.
        string key_global = "global_cache";
        string key_date = "date_cache";
        if (!MemoryCache.TryGetValue<Country[]>(key_countries, out Country[] values) &&
            (!MemoryCache.TryGetValue<Global>(key_global, out Global value) &&
              (!MemoryCache.TryGetValue<DateTime>(key_date, out DateTime date)))) // Verifica se há dados em cache.
        {
          var create = RestService.For<IGetRoot>("https://api.covid19api.com/summary");
          var result = await create.GetAsync(); // sou var result = create.GetAsync().GetAwaiter().GetResult();
          _logger.LogInformation(result.ToString());
          Global = result.Global;
          Countries = result.Countries;
          Date = result.Date;
          MemoryCache.Set(key_countries, Countries, System.TimeSpan.FromMinutes(30)); // Salva os dados em cache e define um tempo de expiração
          MemoryCache.Set(key_global, Global, System.TimeSpan.FromMinutes(30));
          MemoryCache.Set(key_date, Date, System.TimeSpan.FromMinutes(30));
        }
        else
        {
          Global = value;
          Countries = values;
          Date = date;
        }
      }
      catch (Exception e)
      {
        _logger.LogInformation("Erro na requisição http: " + e.Message);
      }
    }
  }
  • you have assigned a value that does not exist! no else of your code value and date is not stating, if you are confused because these values should have their own if and else!

1 answer

0


Note that your variables are in different scopes/blocks, state them before the if-else and correct the TryGetValue by removing the data type after the parameter out.

DateTime date = DateTime.MinValue;
Global value = null;
Country[] values = null;

if (!MemoryCache.TryGetValue<Country[]>(key_countries, out values) &&
    (!MemoryCache.TryGetValue<Global>(key_global, out value) &&
        (!MemoryCache.TryGetValue<DateTime>(key_date, out date)))) // Verifica se há dados em cache.
{
    var create = RestService.For<IGetRoot>("https://api.covid19api.com/summary");
    var result = await create.GetAsync(); // sou var result = create.GetAsync().GetAwaiter().GetResult();
    _logger.LogInformation(result.ToString());
    Global = result.Global;
    Countries = result.Countries;
    Date = result.Date;
    MemoryCache.Set(key_countries, Countries, System.TimeSpan.FromMinutes(30)); // Salva os dados em cache e define um tempo de expiração
    MemoryCache.Set(key_global, Global, System.TimeSpan.FromMinutes(30));
    MemoryCache.Set(key_date, Date, System.TimeSpan.FromMinutes(30));
}
else
{
    Global = value;
    Countries = values;
    Date = date;
}
  • Datetime is not voidable.

  • you’re right. corrected.

  • The first time you enter the site, the request is made and the data is cached, so that the second time you enter the site, you do not need to make another request. But you are making a second request, because you will always set the Date as minimum. And now?

  • This is a problem that existed before the change I suggested, which was to correct the error of "Use of unassigned variable". This behavior that you cite is related to the logic that you adopted to do this type of control, for this, I suggest that you review this method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.