| nt maxValue = (int)Session["maxValue"];
Elsewhere the following code is used to update the value.
Session["maxValue"] = 56.7;
If the code to read the "maxValue" session variable into the maxValue int variable is executed again there will be an InvalidCastException thrown.
Most bugs like this will be identified by unit testing – but not always.
Re-using a Key Unintentionally Even when we define constants on each page for the session keys, it is possible to unintentionally use the same key across pages. Consider the following example:
Code on one page:
private const string edit = "edit";... Session[edit] = true;
Code on a second page, displayed after the first page:
private const string edit = "edit";... if ((bool)Session[edit]) { ... }
Code on a third, unrelated, page:
private const string edit = "edit";... Session[edit] = false;
If the third page is displayed for some reason before the second page is displayed, the value may not be what was expected. The code will probably strill run, but the results will be wrong.
Usually this bug will NOT be picked up in testing. It is only when a user does some particular combination of page navigation (or opening a new browser window) that the bug manifests.
At its worst, no one is aware that the bug has manifested, we may just end up modifying data to an unintended value.
Re-using a Key Unintentionally - again In the example above, the same data type was stored in the session variable. Because there is no type checking of what gets stored, the problem of incompatible data types can also occur.
Code on 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页 |