0

So in this code block from my console application, when ran, should move the 'X' in Class02 up and down when you hit the respective arrow keys but it doesn't, it just stays in place:

class Program
{
    static void Main(string[] args)
    {
        Class01.Function01();
    }
}

class Class01
{
    public int num01 = 5;
    public int num02 = 5;

    public static void Function01()
    {
        while (true)
        {
            Class02.Function02();
        }
    }
}

class Class02
{
    public static void Function02()
    {
        var c1 = new Class01();
        Console.SetCursorPosition(c1.num02, c1.num01);
        Console.Write("X");

        ConsoleKeyInfo keyInfo;
        keyInfo = Console.ReadKey(true);
        switch (keyInfo.Key)
        {
            case ConsoleKey.UpArrow:
                c1.num01--;
                break;
            case ConsoleKey.DownArrow:
                c1.num01++;
                break;
        }
    }
}

I know what's wrong here, the int in Class01 is not being changed in class02. therefore the Cursor Position is still set as 5 5 writing the 'X' in the same place every key stroke.

So, how does one change the value of int num01 in Class02?

Thanks for any help with this.

1
  • Some offtopic code review that does seem pertinent (feel free to disagree): Don't use a numbered system for classes, variables and methods. Give them a meaningful name. Just by name alone, I don't know what num01 and num02 are. But I would immediately interpret X and Y as a set of coordinates. Similarly, compare Class01 and Cursor. Once your codebase expands to business level complexity, or you need to revisit code that you've not touched for a long time, you'll understand why clear naming is needed in order to not have to figure out from scratch what's going on. Commented Nov 6, 2017 at 8:43

2 Answers 2

4

You are always creating a new instance of Class01 in the static method Class02.Function02, therefore the value is always it's default value 5. You could make the numbers static too or you could hold a static instance variable of Class01 in Class02, for example:

class Class02
{
    private Class01 c1 = New Class01();

    public static void Function02()
    {
        Console.SetCursorPosition(c1.num02, c1.num01);
        Console.Write("X");

        ConsoleKeyInfo keyInfo;
        keyInfo = Console.ReadKey(true);
        switch (keyInfo.Key)
        {
            case ConsoleKey.UpArrow:
                c1.num01--;
                break;
            case ConsoleKey.DownArrow:
                c1.num01++;
                break;
        }
    }
}

another option is to pass the instance of Class01 to the method:

public static void Function02(Class01 c1)
{
    Console.SetCursorPosition(c1.num02, c1.num01);
    Console.Write("X");

    ConsoleKeyInfo keyInfo;
    keyInfo = Console.ReadKey(true);
    switch (keyInfo.Key)
    {
        case ConsoleKey.UpArrow:
            c1.num01--;
            break;
        case ConsoleKey.DownArrow:
            c1.num01++;
            break;
    }
}

then you call it in this way:

Class01 c1 = new  Class01();
while (true)
{
    Class02.Function02(c1);
}

If the calling method Function01 would not be static you could pass this.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I see my mistake in all of this, but I got it working now. Still kind of new too all of this but I'm learning.
0

The error is, that in every single call you create a new instance of class01 with the initial values in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.