Question

I have this code:

    If SalaryCbx.Checked = True Then
        fundSalary = "S"  <- Throws an error
    Else
        fundSalary = "N"  <- Throws an error
    End If

SonarQube throws a "Critical" error:

Assignment is not used (Category: Redundancies in Code) resharper-vbnet RedundantAssignment Value assigned is not used in any execution path

Is there a better way to write this kind of logic? I tried a Select Case statement but it also threw the error.

I also use this same code except for different variables/checkboxes and it works fine - no errors.

Update: Here's where I am using it.

    Dim insertQry As String = "INSERT INTO FUND (FUND_ID, FUND_NM, FUND_TICKER_NM, FUND_SALARY_IND, FUND_BONUS_IND, FUND_ALCTN_IND, BEG_DT, END_DT) "
    insertQry &= "          VALUES(@FundID, @fndName, @fndTicker, @fndSalary, @fndBonus, @fndAllocation, @fndBeg, @fndEnd) "

     'Code omitted

        'Declare Connection String
        Using sqlConnection As New SqlConnection(myConn)
            'Declare variable for SQL command
            Using cmd As New SqlCommand(insertQry)
                With cmd
                    .Connection = sqlConnection
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue("@FundID", id)
                    .Parameters.AddWithValue("@fndName", fundName)
                    .Parameters.AddWithValue("@fndTicker", fundTicker)
                    .Parameters.AddWithValue("@fndSalary", fundSalary)
Était-ce utile?

La solution

The message is saying that you're not reading the assigned value in any other part of your code. Possible reasons:

  • You made an mistake (like a copy-and-paste error) and are accidentally reading a different variable where you intend to read this one.

  • The reading code has shadowed the written variable with an identically-named variable in a narrower scope.

  • Old code that no longer needs to be present because nothing is meant to read the variable.

  • False positive in the checker.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top