C
C#9mo 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
daniel29mo ago
$code
MODiX
MODiX9mo 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
Pobiega9mo 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
MODiX9mo 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
Pobiega9mo ago
three of them, then cs, then paste your code, then another three
Angius
Angius9mo ago
```cs code ```
Pobiega
Pobiega9mo ago
the start and end tags must be on new lines
HD-Fr0sT
HD-Fr0sTOP9mo 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
Angius9mo ago
So close lol
HD-Fr0sT
HD-Fr0sTOP9mo 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
Angius9mo ago
Yeah, and what is the issue? The code seems fine as it is
HD-Fr0sT
HD-Fr0sTOP9mo 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
Pobiega9mo 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-Fr0sTOP9mo ago
is it because im not looping the logic inside the method? if yes (slaps face) if no (slaps face twice)
Pobiega
Pobiega9mo ago
use the debugger check what value your key variable has after you press an arrow key.
HD-Fr0sT
HD-Fr0sTOP9mo ago
did it
Pobiega
Pobiega9mo ago
mhm? and what value did it reveal?
HD-Fr0sT
HD-Fr0sTOP9mo 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