none
The name '_random' does not exist in the currnet context. RRS feed

  • Question

  • Hey guys!

    I'm making a spastic mouse prank application.

    I tried to make a randomizer for the 'x' axis, but the error window says:

    The name '_random' does not exist in the current context.

    this is my code:

                int moveX = 0;
                int moveY = 0;
         
    
                while (true)
                {
    
                    moveX = _random.Next(20) - 10;
    
                    Cursor.Position = new System.Drawing.Point(Cursor.Position.X + moveX, Cursor.Position.Y + 10);
                    Thread.Sleep(500);


    can anybody help me?

    Kind regards,

    Tom Vos

    • Moved by Jiayi Li Thursday, November 12, 2015 3:06 AM
    Wednesday, November 11, 2015 1:45 PM

Answers

  • I'm expecting that _random is supposed to be a field of your class of type Random.  (Based on the leading underscore).  You haven't provided the class so I don't know what it is.  It might also need to be static, if this is part of your Program class.

    e.g.: 

    public class Prank
    {
        // ...
    
        Random _random = new Random();
    
        // ...
    }

    In practical terms, you can just add a local Random variable outside your loop to keep track of your Random object.

                int moveX = 0;
                int moveY = 0;
    
                Random _random = new Random();     
    
                while (true)
                {
    
                    moveX = _random.Next(20) - 10;
    
                    Cursor.Position = new System.Drawing.Point(Cursor.Position.X + moveX, Cursor.Position.Y + 10);
                    Thread.Sleep(500);

    • Proposed as answer by DotNet WangModerator Thursday, November 12, 2015 5:30 AM
    • Marked as answer by TomVos Thursday, November 12, 2015 2:28 PM
    Wednesday, November 11, 2015 2:13 PM

All replies

  • I'm expecting that _random is supposed to be a field of your class of type Random.  (Based on the leading underscore).  You haven't provided the class so I don't know what it is.  It might also need to be static, if this is part of your Program class.

    e.g.: 

    public class Prank
    {
        // ...
    
        Random _random = new Random();
    
        // ...
    }

    In practical terms, you can just add a local Random variable outside your loop to keep track of your Random object.

                int moveX = 0;
                int moveY = 0;
    
                Random _random = new Random();     
    
                while (true)
                {
    
                    moveX = _random.Next(20) - 10;
    
                    Cursor.Position = new System.Drawing.Point(Cursor.Position.X + moveX, Cursor.Position.Y + 10);
                    Thread.Sleep(500);

    • Proposed as answer by DotNet WangModerator Thursday, November 12, 2015 5:30 AM
    • Marked as answer by TomVos Thursday, November 12, 2015 2:28 PM
    Wednesday, November 11, 2015 2:13 PM
  • You solved my problem!

    Thank you!

    Thursday, November 12, 2015 2:28 PM