Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
am just trying to do the captcha code in my asp.net page.
but unfortunately am getting the below error,
please help me solve this problem.
Compiler Error Message: CS0118: 'combination' is a 'variable' but is used like a 'method'

Source Error:

Line 99:             for (int i = 0; i <= 5; i++)
Line 100:            {
Line 101:              captcha.Append(combination(random.Next(combination.Length)));
Line 102:             // random.Next(combination.Length);
Line 103:  


my code is:
C#
private void FillCapctha()
{

    try
    {
        Random random = new Random();

        string combination = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

        StringBuilder captcha = new StringBuilder();

        for (int i = 0; i <= 5; i++)
        {
          captcha.Append(combination(random.Next(combination.Length)));
         // random.Next(combination.Length);

        }

        Session["captcha"] = captcha.ToString();


        imgCaptcha.ImageUrl = "GenerateCaptcha.aspx?" + DateTime.Now.Ticks.ToString();


    }
    catch
    {
        throw;
    }

}



thanks to all.
Posted
Updated 4-Sep-15 2:27am
v2

Don;t bother to "brew your own" - it's unlikely to be as good as the "proper" ones, and it'll take you a lot more effort.

Instead, look at using Recaptcha[^] - it's free, very easy to use, nice and secure, and it helps the world digitize information!
 
Share this answer
 
To answer your question you are looking to get the character at the given index of the comination string so you need square brackets for that;

C#
for (int i = 0; i <= 5; i++)
{
    captcha.Append(combination[random.Next(combination.Length)]);
}


However I agree that rolling your own is fairly pointless. It's fine if you're doing this as a learning exercise, but if you want to use it for real you are better using one already written.
 
Share this answer
 
Comments
Richard Deeming 4-Sep-15 8:49am    
Snap!
Sergey Alexandrovich Kryukov 4-Sep-15 11:54am    
5ed.
—SA
The error occurs because you're using VB-style round brackets to return a character from the string. C# uses square brackets:
C#
captcha.Append(combination[random.Next(combination.Length)]);

There's also no point in a try..catch block which simply re-throws the exception.

But, as Griff said, don't roll your own CAPTCHA solution. The existing solutions will be much more secure, and much better tested. With the latest version of reCAPTCHA, the user doesn't even need to try to read some distorted text - they just have to tick a box, which is much less annoying.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Sep-15 11:54am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900