Hello everyone,

I'm trying to make a small application that checks on submission if a user is human or not by just copying what's shown on a page and submitting it for validation. OK, I have a few errors with this piece of code and am unsure how to fix it as I'm very very new to c# community.

// HumanCheckerApp.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class _Default : System.Web.UI.Page
    {
        string genRanNum;
        HumanCheckerService.HumanCheckerApplication humanCheck = new HumanCheckerService.HumanCheckerApplication();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            genRanNum = humanCheck.RandomNumberGenerator();
            //Display generated numbers
            Label1.Text = genRanNum;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Send input to webservice for validation
            Label2.Text = humanCheck.HumanChecker(txtBox.Text);
        }
    }
}










// service1.asmx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService2
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/", Name = "Human Checker Service", Description= "This checks whether a user is human or not.")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string RandomNumberGenerator()
        {
            Random random = new Random();
            string num = "";
            for (int i = 0; i <= 9; i++)
            {
                num += random.Next(0, 9);
            }
            return num;
        }

        [WebMethod]
        public string HumanChecker(string str)
        {
            if (str == this.RandomNumberGenerator())
            {
                return "You are a human.";
            }else{
                return "You aren't a human!";
            }

        }
    }
}

Thanks in advance, folks.

public string HumanChecker(string str) {
    if (str == this.RandomNumberGenerator()) {
        return "You are a human.";
    } else {
        return "You aren't a human!";
    }
}

You do realize that this code is going to generate a new random number and that the odds that it will match what the person typed in are very small?

random.Next(0, 9)

This generates a number from 0 to 8 (read Random.Next). Is this what you wanted?

string num = "";
for (int i = 0; i <= 9; i++) {
    num += random.Next(0, 9);
}

This is a bad way to build a string, use StringBuilder.

One thing we can't do here is read your mind. What errors does it have?

Edited 5 Years Ago by Momerath: n/a

Comments
Not much else to be said other than this =)

Thanks for your reply, buddy. The errors are the followings:
" Error 1 'WebApplication3.HumanCheckerService.HumanCheckerApplication' does not contain a definition for 'RandomNumberGenerator' and no extension method 'RandomNumberGenerator' accepting a first argument of type 'WebApplication3.HumanCheckerService.HumanCheckerApplication' could be found (are you missing a using directive or an assembly reference?) C:\Users\Documents\Visual Studio 2010\Projects\WebService1\WebApplication3\Default.aspx.cs HumanCheckerApplication
"


" Error 2 'WebApplication3.HumanCheckerService.HumanCheckerApplication' does not contain a definition for 'HumanChecker' and no extension method 'HumanChecker' accepting a first argument of type 'WebApplication3.HumanCheckerService.HumanCheckerApplication' could be found (are you missing a using directive or an assembly reference?) C:\Users\Documents\Visual Studio 2010\Projects\WebService1\WebApplication3\Default.aspx.cs HumanCheckerApplication
"


I'd also appreciate it so much if you direct me to an awesome site that teaches C# in easy steps.

Thank you for the help again.

It's been too long, don't remember the directives, here are some guesses.
'WebApplication3.HumanCheckerService.HumanCheckerApplication' does not contain a definition for 'RandomNumberGenerator'
Your web service directives need to define this method. (I thought you did without a type)
no extension method 'RandomNumberGenerator' ... type 'WebApplication3.HumanCheckerService.HumanCheckerApplication'
The (not shown) code is trying to access the web service by passing an argument into the class, the only service (I think) you defined is without parameters. Define a service that accepts that type or change your calling app to not pass an argument.

" Error 2 'WebApplication3.HumanCheckerService.HumanCheckerApplication' does not contain a definition for 'HumanChecker' and no extension method 'HumanChecker' accepting a first argument of type 'WebApplication3.HumanCheckerService.HumanCheckerApplication' could be found (are you missing a using directive or an assembly reference?) "
Now it's looking like maybe when you set up the web service you didn't attach the HumanCheckerApplication class to your application. Where is it defined? Are you sure it is attached? "HumanCheckerService.HumanCheckerApplication humanCheck = new HumanCheckerService.HumanCheckerApplication();" is coming out of left field if the class isn't available. How/Why do you expect it to be defined?

I'd also appreciate it so much if you direct me to an awesome site that teaches C# in easy steps.

Yea, wouldn't that be nice.

it is already defined and attached to the web application. But, I am not sure why the compiler doesn't recognize it.

Sure, it would be nice. Any good help from anyone would be very very much appreciated again.

OK. I have managed to run my little program and am really happy with it after having fixed a silly bug in it. So, now, it doesn't validate the if-statement properly. The condition is that if the typed number is Equal to the given random number then Tell them they're human Or else, otherwise. It jumps straight to the second else-statement even if the typed number is ABSOLUTELY equal to the random number. Any idea why?

Are you sure you have confirmed that both the numbers matches?

Can you try changing your method with this (for testing):

public string HumanChecker(string str) {
    string rnd = this.RandomNumberGenerator();

    if (str == rnd) {
        return "You are a human.";
    } else {
        return "Number you entered: '" + str + "' -- Random number: '" + rnd + "' ...You aren't a human!";
    }
}

I'd also appreciate it so much if you direct me to an awesome site that teaches C# in easy steps.

You can try my C# blog:
http://www.farooqazam.net

Edited 5 Years Ago by farooqaaa: n/a

OK. I type in the showing number and when it evaluates the statement as false then it prints the entered number and the random number(this is new???) how this happens?

Can you post the exact message it shows there?

Are both the "Number you entered:" and "Random Number:" the same?

OK. I type in the showing number and when it evaluates the statement as false then it prints the entered number and the random number(this is new???) how this happens?

I don't understand you.

Edited 5 Years Ago by farooqaaa: n/a

OK. I type the generated/appearing number in the text box and then it gets evaluated as a false statement (not matched for some reason!!!!) and when the method/function prints the generated number in the result. It is different from the appearing one. I will give ya an example of that.

Let's say the number is 112244 .. I type it in the text box then it is not recognized as a matched number. So, the result I get is that
" the entered number is 112244 and the random number is 225543 and You're not a human "

I understand your problem now. Every time you call RandomNumberGenerator() it generates a new number. When you call it to show the random number to the user, it generates a new number. When you compare the user input you generate a yet another random number. That is why it never matches.

You need to generate the number once and keep it in a global variable.

Edited 5 Years Ago by farooqaaa: n/a

@rotten69:
Just as a side note. Are you ever expecting dogs, baboons, mice, ants, little green man from Mars etc., to work with your application?

@ddanbe.. WTF!! if you're not willing to help, it's all good. There will be someone to give GREAT help.

Thanks a lot faroooqa.

I understand your problem now. Every time you call RandomNumberGenerator() it generates a new number. When you call it to show the random number to the user, it generates a new number. When you compare the user input you generate a yet another random number. That is why it never matches.

You need to generate the number once and keep it in a global variable.

Which is what I said in my post way back at the beginning of this thread! Does anyone actually bother to read the replies?

Comments
:)

Which is what I said in my post way back at the beginning of this thread! Does anyone actually bother to read the replies?

I did. I agreed with you, but it was so blatently obvious, I didn't think it needed comment or reinforcement.

This is still flagged as unsolved. Please change the status. (Or explain why it is still unsolved.)

This article has been dead for over six months. Start a new discussion instead.