"(object name) does not exist in the current context" when the item is clearly in scope?

Viewed 178

I am building a Blazor serverside application and I've run into what seems to be a trivial problem, but I can't figure out what's wrong. I have a class called TestMonitorDUT.cs in my ViewModels folder, and a page called TestMonitor.razor. In the razor page, I have the following lines where I simply make a new TestMonitorDUT and try to call a member function:

@code {
TestMonitorDUT DUT = new TestMonitorDUT("1");
DUT.PopulateDictionaries();

In this situation, I get the error message: "The name 'DUT' does not exist in the current context."

I am very confused because DUT is clearly defined right there. Thanks for your time

1 Answers

It is transpiled to:

class X
{
  TestMonitorDUT DUT = new TestMonitorDUT("1");
  DUT.PopulateDictionaries();
}

DUT.PopulateDictionaries(); is a statement where only declarations are expected. It should be inside a method. Like

@code {
  TestMonitorDUT DUT = new TestMonitorDUT("1");
  //DUT.PopulateDictionaries();

  overide void OnInitialized()
  {
    DUT.PopulateDictionaries();
  }
}
Related