开发者

ASP.NET Aspx View - How to Remove Unused Variable Warnings

开发者 https://www.devze.com 2023-02-16 12:32 出处:网络
My company has a zero warning tolerance policy and 开发者_开发问答I have a warning that I just can\'t get to go away. It is showing on the declaration of the string because

My company has a zero warning tolerance policy and 开发者_开发问答I have a warning that I just can't get to go away. It is showing on the declaration of the string because

Warning 1   The variable 'message' is assigned but its value is never used  

This is the code snippet:

<% 
    string message = "Message to be displayed";
    if (Model.Count == 0)
    {
        %>
        No records to display. <%= message %>
        <%
    }
    else
    {
%>

The rest of the code is correct, so it's not a syntax error.

Why would this kind of warning appear in the first place when I am clearly referencing it?

Thx in advance!


It's strange that your company has 0 tolerance to errors and still lets you write C# code in your views which turn them into spaghetti. Also you are hardcoding urls instead of using URL helpers which is very bad.

So you could start by slightly improving the code:

<% if (Model.Count == 0) { %>
    No records to display. 
    <%= Html.ActionLink("Click here to add.", "Details", "ObjectDefinition") %>
<% else { %>
    ...
<% } %>


Change the declare as below, this issue will be fixed.

When you see this kind of warning, just review the declare. It will OK then.

<% 
if (Model.Count == 0)
{
    string message = "Message to be displayed";  // declare message here :)
    %>
    No records to display. <%= message %>
    <%
}
else
{
%>

Best regards.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号