C
C#8mo ago
HD-Fr0sT

Cant methods do ReadKey() and ReadLine() by their own or am i forced to use parameters

I can use parameters but i am making a looped arrow key menu so it would not be optimal
19 Replies
daniel2
daniel28mo ago
$code
MODiX
MODiX8mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Pobiega
Pobiega8mo ago
sure you can call Console.ReadLine() pretty much anywhere. Is it a good idea to do so? Not always. if your program is only ever gonna be a console app, and you dont want a centralized way to change potential styling etc, then its fine
MODiX
MODiX8mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Pobiega
Pobiega8mo ago
three of them, then cs, then paste your code, then another three
Angius
Angius8mo ago
```cs code ```
Pobiega
Pobiega8mo ago
the start and end tags must be on new lines
HD-Fr0sT
HD-Fr0sTOP8mo ago
cs
using ClassFile;

while (true)
{
Console.Clear();

ClassFile.Class.VisaMeny();

}
cs
using ClassFile;

while (true)
{
Console.Clear();

ClassFile.Class.VisaMeny();

}
this is the main
Angius
Angius8mo ago
So close lol
HD-Fr0sT
HD-Fr0sTOP8mo ago
namespace ClassFile;

public class Class
{
public static void VisaMeny()
{
string punkt = "*";

int val = 1;

Console.WriteLine($"{(val == 1 ? punkt : " ")} Val (1) ");
Console.WriteLine($"{(val == 2 ? punkt : " ")} Val (2) ");
Console.WriteLine($"{(val == 3 ? punkt : " ")} Val (3) ");

ConsoleKeyInfo key;
key = Console.ReadKey();

switch (key.Key)
{
case ConsoleKey.DownArrow:
val++;
break;

case ConsoleKey.UpArrow:
val--;
break;

}

switch (val)
{
case >3:
val = 1;
break;

case <1:
val = 3;
break;
}


}
}
namespace ClassFile;

public class Class
{
public static void VisaMeny()
{
string punkt = "*";

int val = 1;

Console.WriteLine($"{(val == 1 ? punkt : " ")} Val (1) ");
Console.WriteLine($"{(val == 2 ? punkt : " ")} Val (2) ");
Console.WriteLine($"{(val == 3 ? punkt : " ")} Val (3) ");

ConsoleKeyInfo key;
key = Console.ReadKey();

switch (key.Key)
{
case ConsoleKey.DownArrow:
val++;
break;

case ConsoleKey.UpArrow:
val--;
break;

}

switch (val)
{
case >3:
val = 1;
break;

case <1:
val = 3;
break;
}


}
}
they are on seperate files
Angius
Angius8mo ago
Yeah, and what is the issue? The code seems fine as it is
HD-Fr0sT
HD-Fr0sTOP8mo ago
when i press the arrow the menu dosent budge seems like the method dosent really listen to readkey i would really like to not use parameters
Pobiega
Pobiega8mo ago
you dont need to. use a breakpoint and the debugger to figure out what is going wrong its not the Console.ReadKey()
HD-Fr0sT
HD-Fr0sTOP8mo ago
is it because im not looping the logic inside the method? if yes (slaps face) if no (slaps face twice)
Pobiega
Pobiega8mo ago
use the debugger check what value your key variable has after you press an arrow key.
HD-Fr0sT
HD-Fr0sTOP8mo ago
did it
Pobiega
Pobiega8mo ago
mhm? and what value did it reveal?
HD-Fr0sT
HD-Fr0sTOP8mo ago
short story it works but i just needed a loop and clear to make it work as i intented
using ClassFile;

while (true)
{
Console.Clear();
ClassFile.Class.VisaMeny();

}
using ClassFile;

while (true)
{
Console.Clear();
ClassFile.Class.VisaMeny();

}
namespace ClassFile;

public class Class
{
public static int VisaMeny()
{

string punkt = "*";

int val = 1;

while (true)
{
Console.Clear();
Console.WriteLine($"{(val == 1 ? punkt : " ")} Val (1) ");
Console.WriteLine($"{(val == 2 ? punkt : " ")} Val (2) ");
Console.WriteLine($"{(val == 3 ? punkt : " ")} Val (3) ");

ConsoleKeyInfo key;
key = Console.ReadKey();

int vald;

switch (key.Key)
{
case ConsoleKey.DownArrow:
val++;
break;

case ConsoleKey.UpArrow:
val--;
break;

case ConsoleKey.Enter:
vald = val;
return vald;

}

switch (val)
{
case >3:
val = 1;
break;

case <1:
val = 3;
break;
}
}




}
}
namespace ClassFile;

public class Class
{
public static int VisaMeny()
{

string punkt = "*";

int val = 1;

while (true)
{
Console.Clear();
Console.WriteLine($"{(val == 1 ? punkt : " ")} Val (1) ");
Console.WriteLine($"{(val == 2 ? punkt : " ")} Val (2) ");
Console.WriteLine($"{(val == 3 ? punkt : " ")} Val (3) ");

ConsoleKeyInfo key;
key = Console.ReadKey();

int vald;

switch (key.Key)
{
case ConsoleKey.DownArrow:
val++;
break;

case ConsoleKey.UpArrow:
val--;
break;

case ConsoleKey.Enter:
vald = val;
return vald;

}

switch (val)
{
case >3:
val = 1;
break;

case <1:
val = 3;
break;
}
}




}
}
tl:dr i have major brain damage
Want results from more Discord servers?
Add your server