2

In the following code, no matter what I try, the name strSide "does not exist in the current context", when I place a break point as shown. I want strSide to contain the last character in rawId and then to strip that character so that the value of rawId will convert to an integer. I don't get a compiler error, but do get a runtime error.

When the value of rawId is 8429R, the stripping works, but I cannot get the R assigned to strSide.

        foreach (string fieldName in Request.QueryString)
        {
                rawId = fieldName;
                String strSide = Convert.ToString(rawId[rawId.Length - 1]); <-- name does not exist
                if (!IsNumeric(rawId)) <--break point set here.
                {
                    rawId = StripRightChar(rawId);  
                }
                Qty = Request.QueryString[fieldName];
            int productId = 0;
            if (fieldName == "txtReference")
            {
                strComments = Request.QueryString[fieldName];
            }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Can't you just hover the mouse over `rawId` and see what character is last in that string? Or "Add watch" on `rawId`. Your statement `String strSide = Convert.ToString(rawId[rawId.Length - 1]);` doesn't do anything. Why do you have it? PS! Another way to get the last character as a length-1 string is `strSide = rawId.Substring(rawId.Length - 1);`. – Jeppe Stig Nielsen Sep 28 '13 at 20:35

2 Answers2

4

Might the variable have been "optimized away"? It is not used anywhere after its creation. See this for details: How to prevent C# compiler/CLR from optimizing away unused variables in DEBUG builds?.

Community
  • 1
  • 1
meilke
  • 3,280
  • 1
  • 15
  • 31
0

Do you need the value stored in strSide?

If not you could try this alternative solution

Update, since you need the value, try this:

var s = "8429L";
var numbers = s.Substring(0, s.IndexOfAny("RLB".ToCharArray()));
var letter = s.Substring(numbers.Length);
Community
  • 1
  • 1
Mårten
  • 349
  • 1
  • 10
  • Yes, I need the value. It will either be "R" (right), "L" (left), or "B" (both). I need to pass the numeric part as a product ID and the char part as an indicator which side(s) of the part is needed. Here are the methods I've tried for extracting the character //String strSide = Convert.ToString(rawId[rawId.Length - 1]); //Char strSide = rawId[rawId.Length - 1]; //String strSide = rawId.Substring(rawId.Length - 1, 1); char strSide = Convert.ToChar(rawId.Substring(rawId.Length - 1, 1)); –  Sep 28 '13 at 20:26
  • Added another suggestion to my answer, not an answer to your original question to why you can't debug though. – Mårten Sep 28 '13 at 20:45