1,105,229 Community Members

Confused about a logical paradox

Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

It's been a long while since I've last been here but I've spent the last 2 hours pulling my hair out trying to figure this out and I'm hoping for a bit of help with this one.

I have a question/response check (a human verification check) on my page and it refuses to work... I've written the code such that it generates the question and answer on Page_Load only on the first (non postback) load. I've generated a label which has it's text property pulled from the Question as generated on Page_Load and I'm trying to validate the answer from the generated answer from Page_Load. For some reason it always validates as false even if I put the answer straight from the expected answer into the text box I'm validating from.

Question/Answer Generation:

private string hQuest;
    private string hAns;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            humQChange();
        }
    }

    private void humQChange()
    {
        humScript regHum = new humScript();
        hQuest = regHum.getHQuestion();
        hAns = regHum.getHAnswer();
        hQuestionLabel.Text = hQuest;
    }

Validation:

private bool checkHumResp()
    {
        if (hAnswer.Text == hAns)
        {
            return true;
        }
        else
        {
            testLabel.Text = "Failed";
            return false;
        }
    }

Even if I put a line of code in that tells it to fill hAnswer.Text with the value from hAns I still get a failure when validating one against the other. Both are string based variables so this isn't making any kind of sense to me.

Any suggestions?

Member Avatar
hericles
Veteran Poster
1,078 posts since Nov 2007
Reputation Points: 98 [?]
Q&As Helped to Solve: 232 [?]
Skill Endorsements: 14 [?]
Featured
 
2
 

First thing to do would be to debug the code at the line comparing the two. Visually confirm they are the same.

Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

Well, as I said, if I insert a line that says hAnswer.Text = hAns it then places the value into hAnswer... following that, the comparison of hAnswer.Text to hAns should match but it still doesn't for some reason.

Member Avatar
Consider
Newbie Poster
4 posts since Mar 2012
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
2
 

Even though hAnswer.Text and hAns is the same on the first trip, you would still want to do a walk through of the code to make sure that the value is not changing when the page is posted back.

Have you tried debugging it to make sure the logic is flowing correctly?

Member Avatar
|-|x
Posting Whiz
353 posts since Apr 2008
Reputation Points: 113 [?]
Q&As Helped to Solve: 56 [?]
Skill Endorsements: 8 [?]
 
1
 

It is possible that the page variable hAns is losing its value on postBack. You could try storing this in a Session or Viewstate variable instead.

However, as the other guys have suggested, you should place a breakpoint on the line of code for the comparison and check what value each has at that time. Making the assumption that a variable/object has a value because you have set a value at some previous time is not necessarily valid.

Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

You know... I never thought about that... the thing I've been doing is generating (for testing sake) the answer into the hAnswer.Text at the point it's generated. It never occurred to me that during the postback of the page it might clear the variable...

I always kind of figured the point of a locally stored variable was to store info between states lol

Member Avatar
Lusiphur
Practically a Posting Shark
852 posts since Jun 2010
Reputation Points: 185 [?]
Q&As Helped to Solve: 134 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

Oh snap! I forgot to close this.

Yeah the issue was me being out of practice and completely forgetting that, while a C# program will carry forward variables from state to state, a web-based asp.net script will not carry said variables to the next page state without storing them in some way between states.

Being lazy, I'll just store the "answer" in an invisible field on the page as it's only a "Human Verification" deal and not of any grave security concern.

Thanks again for the help folks :D

Question Answered as of 2 Years Ago by Consider, hericles and |-|x
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: