Reputation: 383
I have a repeating code here, full of goto
statements that make this while loop well... Repeat forever.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
main();
}
public static ConsoleKeyInfo keyPressed;
private static void main()
{
start:
keyPressed = Console.ReadKey();
while (true)
{
loopstart:
if (keyPressed.Key == ConsoleKey.Enter)
{
Console.WriteLine("You pressed the Enter Key!");
goto loopstart;
}
if (keyPressed.Key == ConsoleKey.Escape)
{
Console.WriteLine("You pressed the Escape Key!");
goto loopstart;
}
if (keyPressed.Key == ConsoleKey.Spacebar)
{
Console.WriteLine("You pressed the Spacebar!");
goto loopstart;
}
else
{
break;
}
}
Console.WriteLine("You broke the loop!");
goto start;
}
}
}
Without removing any code, is it possible to change the value of keyPressed.Key
or keyPressed
itself to NULL
; the state it was when it was declared, or to any other value/key that's not the spacebar, enter or escape key?
Of course, the problem could be solved by removing all the goto loopstart;
in the code, but that's against the point of the question.
What I want to do is make the keyPressed.Key
value NULL
(or any other value) so that all the IF
statements will result in false, which therefore means not running the goto loopstart
code.
The problem now is that when I try to nullify it with a simple keyPressed = null;
, it comes with the error of:
Cannot convert null to 'System.ConsoleKeyInfo' because it is a non-nullable value type.
Is there any way I can nullify (or change the value to something else) so that I can break the loop?
(As in: Make the IF
statement get to the point where it has to run the else
code)
It should look something like:
...
{
loopstart:
if (keyPressed.Key == ConsoleKey.Enter)
{
Console.WriteLine("You pressed the Enter Key!");
// keyPressed = null; <-- Does not work.
// Do something to make ConsoleKey.Key to equal something else.
goto loopstart;
}
if (keyPressed.Key == ConsoleKey.Escape)
{
Console.WriteLine("You pressed the Escape Key!");
...
Obviously with the // Do something to make ConsoleKey.Key to equal something else.
replaced with working code?
If this works, the first time the loop runs (presuming the key pressed at the start is either the Spacebar, Escape or Enter keys) would result with the goto loopstart;
being used, and the second time round would skip through to the goto start;
where it'll ask for another key.
And then the process repeats at the speed of which the user gives an input, rather than repeating with the same key without stop, or asking for another key.
Basically: Make the loop run the IF
statement as a proper IF
statement instead of a FOR
loop.
Upvotes: 1
Views: 2262
Reputation: 28355
Why use goto
-statement, it's very outdated constructure. You can easily continue
the loop. And else
check is also redundant. You can simply read the key from Console
before check, like this:
while (true)
{
keyPressed = Console.ReadKey();
switch (keyPressed.Key)
{
case ConsoleKey.Enter:
Console.WriteLine("You pressed the Enter Key!");
continue;
case ConsoleKey.Escape:
Console.WriteLine("You pressed the Escape Key!");
continue;
case ConsoleKey.Spacebar:
Console.WriteLine("You pressed the Spacebar!");
continue;
}
// should be outside the switch for breaking the loop
break;
}
If you want to clear keyPressed
, use default
construction, like this:
keyPressed = default(ConsoleKeyInfo);
But why do you want to do this? Garbage Collection will clear the memory by itself, you should not go into there.
Upvotes: 5
Reputation: 956
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
main();
}
public static ConsoleKeyInfo keyPressed;
private static void main()
{
start:
keyPressed = Console.ReadKey();
while (true)
{
loopstart:
if (keyPressed.Key == ConsoleKey.Enter)
{
Console.WriteLine("You pressed the Enter Key!");
keyPressed = new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false);
goto loopstart;
}
if (keyPressed.Key == ConsoleKey.Escape)
{
Console.WriteLine("You pressed the Escape Key!");
goto loopstart;
}
if (keyPressed.Key == ConsoleKey.Spacebar)
{
Console.WriteLine("You pressed the Spacebar!");
goto loopstart;
}
else
{
break;
}
}
Console.WriteLine("You broke the loop!");
goto start;
}
}
}
Upvotes: 2