Home > Back-end >  Blazor parameter giving Cannot use Local Variable before it is declared error
Blazor parameter giving Cannot use Local Variable before it is declared error

Time:01-13

I have some components that accept a Name string and will display different information based on the name passed them. Some of these components have no issues, but some of them will flag a CS0841 error code on the usage.

Error (active) CS0841 Cannot use local variable 'Name' before it is declared \Pages\Awards\Tech \Pages\Awards\Tech\Profile.razor 4

This one Works

<!-- Block1 -->
<div  onclick="@callback">
    <div>
        <Images Name="@Name"/>
    </div>
    <div>
        <h2><NameInfo Name="@Name" ReturnVal="Name"/></h2>
        <h3>@year</h3>
    </div>
</div>
@code {
    [Parameter]
    public string Name{get;set;}
    [Parameter]
    public string? Classes{ get; set; }
    [Parameter]
    public string[] list{ get; set; }
    [Parameter]
    public int year{ get; set; }
    [Parameter]
    public EventCallback<int> yearCallback{ get; set; }


    public async Task callback()
    {
        await yearCallback.InvokeAsync(year);
    }
}


But this one doesn't

<!-- Block2 -->
<div >
    <div >
        <h2>@Year</h2>
        <h1><NameInfo Name="@Name" ReturnVal="Name" /></h1>
    </div>
    <div >
        <Images Name="@Name"/>
    </div>
    <button  onclick="@callback"> X </button>
</div>

@code {
    [Parameter]
    public string Name{ get; set; }

    [Parameter]
    public string? Year{ get; set; }

    [Parameter]
    public EventCallback<string> clear{ get; set; }

    public async Task callback()
    {
        await clear.InvokeAsync("");
    }
}

I've searched the error code information, looked at several other questions on here, the closest seemingly here. However, this doesn't explain why I'm getting this error. It applies to all instances of calling my parameters, in block2, but not in block1.

Block1 compiles with no issues, links with no issues and runs fine. Block2 throws errors and prevents successful build.

What am I missing that's causing this to happen?

Edit: Corrected EventCallback to EventCallback and added ("") to InvokeAsync in block2

CodePudding user response:

I'm uncertain as to the source of the issue. I did, however, determine a fix that allowed it to build again.

  1. In HTML remove all references to parameters.
<!-- Block2 -->
<div >
    <div >
        <h2></h2>
        <h1><NameInfo Name="" ReturnVal="Name" /></h1>
    </div>
    <div >
        <Images Name="@Name"/>
    </div>
    <button  onclick=""> X </button>
</div>

@code {
    [Parameter]
    public string Name{ get; set; }

    [Parameter]
    public string? Year{ get; set; }

    [Parameter]
    public EventCallback<string> clear{ get; set; }

    public async Task callback()
    {
        await clear.InvokeAsync("");
    }
}
  1. Cut the code block entirely. Save and build. The same error still appears.
<!-- Block2 -->
<div >
    <div >
        <h2></h2>
        <h1><NameInfo Name="" ReturnVal="Name" /></h1>
    </div>
    <div >
        <Images Name=""/>
    </div>
    <button  onclick=""> X </button>
</div>

  1. Paste or Retype the code block
<!-- Block2 -->
<div >
    <div >
        <h2></h2>
        <h1><NameInfo Name="" ReturnVal="Name" /></h1>
    </div>
    <div >
        <Images Name=""/>
    </div>
    <button  onclick=""> X </button>
</div>

@code {
    [Parameter]
    public string Name{ get; set; }

    [Parameter]
    public string? Year{ get; set; }

    [Parameter]
    public EventCallback<string> clear{ get; set; }

    public async Task callback()
    {
        await clear.InvokeAsync("");
    }
}
  1. Save and close the IDE
  2. Add references back into the HTML.
<!-- Block2 -->
<div >
    <div >
        <h2>@Year</h2>
        <h1><NameInfo Name="@Name" ReturnVal="Name" /></h1>
    </div>
    <div >
        <Images Name="@Name"/>
    </div>
    <button  onclick="@callback"> X </button>
</div>

@code {
    [Parameter]
    public string Name{ get; set; }

    [Parameter]
    public string? Year{ get; set; }

    [Parameter]
    public EventCallback<string> clear{ get; set; }

    public async Task callback()
    {
        await clear.InvokeAsync("");
    }
}

Code looks identical, but it builds without error now.

  • Related