Friday, April 8, 2011

[c#] The name 'loginrand' does not exist in the current context

hi every one , i just try to create bot for travian , i find some login code

        //download html 
        WebClient client = new WebClient();
        client.Encoding = System.Text.Encoding.UTF8;
        string source = client.DownloadString(@"Http://" + server + "/login.php");
        source = source.Replace("\"", "");

        //search for user

        Regex rloginName = new Regex(@"name=login value=\w*");
        Match mloginName = rloginName.Match(source);
        if (mloginName.Success)
        {
            string loginName = mloginName.ToString().Replace(@"name=login value=", "");
        }

        //search for pass

        Regex rloginPassword = new Regex(@"type=password name=\w*");
        Match mloginPassword = rloginPassword.Match(source);
        if (mloginPassword.Success)
        {
            string loginPassword = mloginPassword.ToString().Replace(@"type=password name=", "");
        }

        Regex rloginUsername = new Regex(@"input class=fm fm110 type=text name=\w*");
        Match mloginUsername = rloginUsername.Match(source);
        if (mloginUsername.Success)
        {
            string loginUsername = mloginUsername.ToString().Replace(@"input class=fm fm110 type=text name=", "");
        }
        Regex rloginRand = new Regex(@"<p align=center><input type=hidden name=\w*");
        Match mloginRand = rloginRand.Match(source);
        if (mloginRand.Success)
        {
            string loginrand = mloginRand.ToString().Replace("<p align=center><input type=hidden name=", "");
        }

        string postString = @"w=1024:768&login=" + loginName + "&" + loginUsername + "=" + user + "&" + loginPassword + "=" + pass + "&" + loginrand + "=&s1.x=10&s1.y=10&s1=login";


        WebRequest req = WebRequest.Create(@"http://" + server + "/dorf1.php");
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postString);
        req.ContentLength = bytes.Length;
        req.ContentLength = bytes.Length;
        Stream os = req.GetRequestStream();
        os.Write(bytes, 0, bytes.Length);
        os.Close();
        WebResponse resp = req.GetResponse();
        StreamReader sr = new StreamReader(resp.GetResponseStream());

but when i try to run i see this error ,

Form1.cs(67,144): error CS0103: The name 'loginrand' does not exist in the current context error CS0103: The name 'loginPassword' does not exist in the current context error CS0103: The name 'loginUsername' does not exist in the current context error CS0103: The name 'loginName' does not exist in the current context

can some one help

From stackoverflow
  • These variables are declared in an IF block, so they are only known there. Declare them before the if block, for example:

    string loginrand;
    if (mloginRand.Success)
    {
        loginrand = mloginRand.ToString().Replace("<p align=center><input type=hidden name=", "");
    }
    

    Maybe read a good C# book before attempting to tweak some code.

    Marc Gravell : For definite assignment, you'd need `string loginrand = "";`
    1. Don't be annoying and create bots for an online game.
    2. Learn about variable scoping, declaring a variable in an if statement means that variable isn't available outside of it.

    For example:

    if (mloginPassword.Success)
    {
        // variable defined here
        string loginPassword = 
            mloginPassword.ToString().Replace(@"type=password name=", "");
    }
    // variable not available here
    
    Kent Boogaart : +1 for your first point.
    JohannesH : Agreed, good first point. But what is the matter with learning about variable scoping through StackOverflow instead of reading it in a book? It is a programming question isn't it? ;)
  • You're defining these strings within the scope of the if block surrounding them. Fall out of the if and you also drop your strings.

    Simply move your declaration for these to the top of the code.

  • hi , i do this

        string loginrand;
    if (mloginRand.Success)
    {
        loginrand = mloginRand.ToString().Replace("<p align=center><input type=hidden name=", "");
    }
    

    but nothing change :(

    ------ Build started: Project: travian-ah, Configuration: Debug Any
    

    CPU ------ C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug+ /debug:full /optimize- /out:obj\Debug\travian-ah.exe /resource:obj\Debug\travian_ah.Form1.resources /resource:obj\Debug\travian_ah.Properties.Resources.resources /target:winexe Form1.cs Form1.Designer.cs Program.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\travian-ah\travian-ah\Form1.cs(33,64): error CS0165: Use of unassigned local variable 'server' C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\travian-ah\travian-ah\Form1.cs(70,56): error CS0165: Use of unassigned local variable 'loginName' C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\travian-ah\travian-ah\Form1.cs(70,74): error CS0165: Use of unassigned local variable 'loginUsername' C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\travian-ah\travian-ah\Form1.cs(70,96): error CS0165: Use of unassigned local variable 'user' C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\travian-ah\travian-ah\Form1.cs(70,109): error CS0165: Use of unassigned local variable 'loginPassword' C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\travian-ah\travian-ah\Form1.cs(70,131): error CS0165: Use of unassigned local variable 'pass' C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\travian-ah\travian-ah\Form1.cs(70,144): error CS0165: Use of unassigned local variable 'loginrand'

0 comments:

Post a Comment