IF statement doing the impossible?

D

DGibson

The behavior of my InfoPath form should be impossible, but it's happening.
There's something that I'm missing that's keeping this from working right.

Background:
There is a Status field in my form that I use to keep track of what step of
a process the form is in. When the form is first opened, this Status field
is given a default value of "New". Then as certain steps are taken, the
Status is updated to different values. Each step of the process has the form
saved to a different Sharepoint document library to await the next step. The
save portion of the form code works fine, but I need to also delete the form
from the old document library.

In my save function, I do a test of the status field to see if it's a new
form to see if there is an old form to delete. The code for this is:

XDocument.UI.Alert("oldDocLib: " + oldDocLib);
if (oldDocLib = "New")
{
XDocument.UI.Alert("New document. No delete needed.");
}
else
{
XDocument.UI.Alert("Deleting from " + oldDocLib);
}

When I first open the form and submit it, it displays the alert "oldDocLib:
New" and then I get another alert "New document. No delete needed." That's
expected and everything is fine there.
I close Infopath and go to the new document library where the form has been
saved to and open it. I take the next step and submit it (which uses a
different submit button and function that calls the same above save
function). This time, I get an alert "oldDocLib: Review" and then the next
alert is "New document. No delete needed."

This is where I'm confused. The IF statement is based on a variable that
I'm alerting off of just before and the variable is obviously not "New", but
no matter what happens, the IF statement is *always* testing true for some
reason.

Can anyone even take a shot in the dark on this one?
 
S

S.Y.M. Wong-A-Ton

Instead of

if (oldDocLib = "New")

try

if (oldDocLib == "New")


Notice the "==" instead of the "="?
 
D

DGibson

Yes, that does fix it. Thank you very much!

Now the hard question, why does it exhibit that behavior?

Does "if (oldDocLib = "New")" reset the value of the variable and then
perform the comparison test?
 
S

S.Y.M. Wong-A-Ton

Hard question? :)

"=" is an assignment operator
"==" is a comparison operator

By using oldDocLib = "New" you gave oldDocLib the value "New" instead of
comparing it to "New", which is why your test failed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top