1

I am receiving an error of CS0103 - 'The name 'newHotel' does not exist in the current context':

I've created a class called 'Hotel', and a property called 'HotelName'.
I've created a variable called 'newHotel', which is of type Hotel.
And I have initialised the variable with a value of "Paradise Beach".

@page "/hotels"


<h3>Hotels</h3>
<p>
    Hotel Name: @newHotel.HotelName // **THIS IS WHERE THE ERROR IS SHOWN IN VS2019**
</p>

Remaining code:

@code {

    protected override void OnInitialized()
    {
        base.OnInitialized();

        Hotel newHotel = new Hotel 
        {
            HotelName = "Paradise Beach"
        };
    }

    class Hotel
    {
        public string HotelName { get; set; }
    }
}

I can't tell where I am going wrong?

Many thanks,

Luxx
  • 13
  • 1
  • 6

2 Answers2

4

It is because newHotel is a local variable declared in the OnInit method so it is out of scope. Add a property above the OnInitialized method for newHotel.


@code {

    ///Added property
    Hotel newHotel { get; set; } = new Hotel();

    protected override void OnInitialized()
    {
        base.OnInitialized();

        newHotel = new Hotel 
        {
            HotelName = "Paradise Beach"
        };
    }

    class Hotel
    {
        public string HotelName { get; set; }
    }
}

Edit: The next issue you will encounter is a NullReference error for newHotel due to the page trying to render before the OnInit method runs and newHotel is initialized. One option is to add a null check for newHotel another option is to initialize it when it is being declared.


@page "/hotels"


<h3>Hotels</h3>

@if(newHotel != null)
{
<p>
    Hotel Name: @newHotel.HotelName
</p>
}

Wayne Davis
  • 394
  • 2
  • 5
  • oh I see!! So basically I had it so that when the component is initialised it creates the variable 'newHotel', but because the component hadn't been rendered at that point, the local variable hadn't been declared. Thank you so much for your comment. – Luxx Feb 25 '21 at 20:22
  • I'm really struggling to understand why I'm instantiating newHotel twice, and why I need get/set on the new obj, so I removed {get; set;} and just had `Hotel newHotel = new Hotel();` , and then also removed `newHotel = new Hotel`, and instead just set the hotel name, so `newHotel.HotelName = "Paradise Beach"`…. and it works. Is there a reason why I should use get/set on newHotel, or why I should instantiate it twice? – Luxx Feb 26 '21 at 07:44
  • Instantiating newHotel when it is being declared will prevent the NullRef exception when this page loads. If you do not do this then you have to make certain you check newHotel for null when it is used with the html, then when newHotel is instantiated it will render. This all comes down to the fact that the page will render without waiting for the life cycle event to occur. This post here talks about the blazor component rendering twice which you may or may not have noticed https://stackoverflow.com/questions/58075628/why-are-blazor-lifecycle-methods-getting-executed-twice. – Wayne Davis Feb 26 '21 at 11:48
  • Also, you do not need to keep base.OnInitialized(); inside the OnInitialized() method. Here is another link that will describe more on the life cycle events https://learn.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-5.0 – Wayne Davis Feb 26 '21 at 11:52
  • As to your first comment, when you declared ```Hotel newHotel = new Hotel() ``` you were creating a local variable that was only available within the scope of the OnInit method. Until you declared newHotel as a property of this component, newHotel was not accessible anywhere else in this component – Wayne Davis Feb 26 '21 at 11:54
  • brilliant, thank you for taking the time to reply back! – Luxx Feb 27 '21 at 21:53
1

I don't know if this has been resolved or not. For me, deleting the '.vs' folder did the trick. I don't actually think it's something in the code. It might be that Visual Studio acts up after updating to new versions.