0

I have a string called strClientNumber that is being used to capture a value in a loop, but outside the loop the string becomes NULL after the assigning object is eliminated. What am I missing?

bool blnAbsent = true;
string strClientNumber = "";
foreach (SPListItem item in varCustomerNumbers)
{
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(item["CustomerNumbers"].ToString());
    XmlNodeList nodelist = xml.GetElementsByTagName("user");

    foreach (XmlNode varUser in nodelist)
    {
        if (clientnumber == varUser.InnerText)
        {
            blnAbsent = false;
            strClientNumber = varUser.InnerText;
            this.Controls.Add(new LiteralControl("   <tr><td>" + varUser.InnerText + "</td><td><input name=\"\" type=\"checkbox\"\n"));
            if (varUser.Attributes["parent"].InnerText == "true")
                this.Controls.Add(new LiteralControl(" checked\n"));
            this.Controls.Add(new LiteralControl("/></td><td><input name=\"\" type=\"checkbox\"\n"));
            if (varUser.Attributes["national"].InnerText == "true")
                this.Controls.Add(new LiteralControl(" checked\n"));
            this.Controls.Add(new LiteralControl("/></td></tr>\n"));
        }
    }
}

if (blnAbsent == true)
{
    this.Controls.Add(new LiteralControl("   <tr><td>" + strClientNumber + "</td><td><input name=\"\" type=\"checkbox\" /></td><td><input name=\"\" type=\"checkbox\" /></td></tr>\"\n"));
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
detailCode
  • 537
  • 1
  • 8
  • 22
  • What does "outside the loop" means? At which point are you checking the value of the variable? – Konamiman May 22 '14 at 15:25
  • Notice I assign the variable at the line of code: strClientNumber = varUser.InnerText; then later reference it this.Controls.Add(new LiteralControl(" " + strClientNumber ... – detailCode May 22 '14 at 15:27
  • 1
    what is clientnumber? if you already have it "clientnumber == varUser.InnerText" then why not just set it directly? – Steve Mitcham May 22 '14 at 15:27
  • First, make sure the value is actually being assigned. Then, I would try to assign it inside the loop with a .ToString(), see if that helps – GEEF May 22 '14 at 15:27
  • if clientnumber is an int, then the if statement will always fail so you never set the variable – Steve Mitcham May 22 '14 at 15:27
  • @VP. strClientNumber is being assigned here: strClientNumber = varUser.InnerText; – detailCode May 22 '14 at 15:30
  • The type of variable that clientnumber will help to determine what is going on with the code. – Steve Mitcham May 22 '14 at 15:35
  • 1
    if `varUser.InnerText` is null then the behaviour of your code makes perfect sense. Are you sure clientnumber is not null? – Kevin May 22 '14 at 15:36
  • @Kevin It's null when I debug. – detailCode May 22 '14 at 15:37
  • So the real problem is that clientnumber is null? – Kevin May 22 '14 at 15:37
  • @Kevin No, when I debug clientnumber is not null. It loops through the code just fine and strClientNumber is assigned the value of varUser.InnerText. When the loop finishes, varUser.InnerText is null so strClientNumber becomes null. – detailCode May 22 '14 at 15:39
  • changing the value of varUser.InnerText will have no effect on strClientNumber since they are both strings. C# doesn't create a reference pointer like that the string is immutable so you would have to trigger the assignment. From the code you posted, I can't see how that behavior will result if clientnumber isn't already null – Steve Mitcham May 22 '14 at 15:43
  • @SteveMitcham I agree... I don't see anyway for the posted code to give the described output. – Kevin May 22 '14 at 15:50
  • 2
    if blnAbsent is true, then the code supposed to assign strClientNumber was never reached to begin with. – Dtex May 22 '14 at 15:55
  • @Dtex Exactly. That might be the answer, simply. In that case the `strClientNumber` will be `""`, not `null` as claimed, but those two values are equivalent when concatenating `" " + strClientNumber + "<...`. – Jeppe Stig Nielsen May 22 '14 at 16:09

2 Answers2

0

If you review the comments, you will see that the poster accepted the hints in the discussion as correct; however, the solution in the posters answer is likely to result in incorrect behavior. The following solution will most likely produce the results that the poster was looking for with this assumption that clientnumber is known ahead of time and is not null.

bool blnAbsent = true;
string strClientNumber = "";
foreach (SPListItem item in varCustomerNumbers)
{
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(item["CustomerNumbers"].ToString());
    XmlNodeList nodelist = xml.GetElementsByTagName("user");

    foreach (XmlNode varUser in nodelist)
    {
        if (clientnumber == varUser.InnerText)
        {
            blnAbsent = false;
            this.Controls.Add(new LiteralControl("   <tr><td>" + varUser.InnerText + "</td><td><input name=\"\" type=\"checkbox\"\n"));
            if (varUser.Attributes["parent"].InnerText == "true")
                this.Controls.Add(new LiteralControl(" checked\n"));
            this.Controls.Add(new LiteralControl("/></td><td><input name=\"\" type=\"checkbox\"\n"));
            if (varUser.Attributes["national"].InnerText == "true")
                this.Controls.Add(new LiteralControl(" checked\n"));
            this.Controls.Add(new LiteralControl("/></td></tr>\n"));
        }
    }
}

if (blnAbsent == true)
{
    this.Controls.Add(new LiteralControl("   <tr><td>" + clientnumber + "</td><td><input name=\"\" type=\"checkbox\" /></td><td><input name=\"\" type=\"checkbox\" /></td></tr>\"\n"));
}
Steve Mitcham
  • 5,268
  • 1
  • 28
  • 56
  • clientnumber is already a string. The rest of the code works just fine. When I debug, I'm interesting in strClientNumber. strClientNumber is assigned inside the second foreach loop. When I debug, it has the correct value. When the loop finishes, strClientNumber becomes NULL because varUser.InnerText which assigned strClientNumber it's value no longer exists. – detailCode May 22 '14 at 15:35
  • Please review the discussion on this question to determine the actual fix for the problem. this answer, while accepted by the poster does not resolve the original issue. – Steve Mitcham May 22 '14 at 21:09
0

I'm such a noob...thanks everyone.

bool blnAbsent = true;
string strClientNumber = "";
foreach (SPListItem item in varCustomerNumbers)
{
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(item["CustomerNumbers"].ToString());
    XmlNodeList nodelist = xml.GetElementsByTagName("user");

    foreach (XmlNode varUser in nodelist)
    {
        strClientNumber = clientnumber;
        if (clientnumber == varUser.InnerText)
        {
            blnAbsent = false;
            this.Controls.Add(new LiteralControl("   <tr><td>" + varUser.InnerText + "</td><td><input name=\"\" type=\"checkbox\"\n"));
            if (varUser.Attributes["parent"].InnerText == "true")
                this.Controls.Add(new LiteralControl(" checked\n"));
            this.Controls.Add(new LiteralControl("/></td><td><input name=\"\" type=\"checkbox\"\n"));
            if (varUser.Attributes["national"].InnerText == "true")
                this.Controls.Add(new LiteralControl(" checked\n"));
            this.Controls.Add(new LiteralControl("/></td></tr>\n"));
        }
    }
}

if (blnAbsent == true)
{
    this.Controls.Add(new LiteralControl("   <tr><td>" + strClientNumber + "</td><td><input name=\"\" type=\"checkbox\" /></td><td><input name=\"\" type=\"checkbox\" /></td></tr>\n"));
}
detailCode
  • 537
  • 1
  • 8
  • 22