𝒲𝒾𝒢𝓏𝒢𝓇𝓇
𝒲𝒾𝒢𝓏𝒢𝓇𝓇
CC#
Created by 𝒲𝒾𝒢𝓏𝒢𝓇𝓇 on 1/18/2024 in #help
Giving parameters to a Func.
Hey, I am trying to make a command line that gets a string and the I call a function based off of said string. I am trying to use dictionaries because I think that'll be better than a super long list of if statements. But, I have run into an issue where I cannot figure out how to give a Funt parameters. Here's the code:
namespace example
{
public static class CommandLine
{
// Dictionary with all valid commands and then the corresponding method.
private static Dictionary<string, Func<string>> validCommands = new Dictionary<string, Func<string>>()
{
{"test", TestMethod}
};

// Called from else where in the program with the raw command string. Ex: "/test"
public static string CommandParser(string rawCommand)
{
if (rawCommand != "")
{
// Removes the '/' from the string and split it to an array of words.
string[] formattedCommand = rawCommand.Remove(0, 1).Split(" ");
try
{
// Finds the command and runs the appropiate function.
return $"Output: {validCommands[formattedCommand[0]].Invoke()}";
}
catch (KeyNotFoundException)
{
// If command isn't found this will be printed.
return $"Invalid command \"{rawCommand}\".";
}
}
else
{
// Returns an empty string to allow for new lines.
return "";
}
}

public static string TestMethod()
{
return "Yay it worked";
}
}
}
namespace example
{
public static class CommandLine
{
// Dictionary with all valid commands and then the corresponding method.
private static Dictionary<string, Func<string>> validCommands = new Dictionary<string, Func<string>>()
{
{"test", TestMethod}
};

// Called from else where in the program with the raw command string. Ex: "/test"
public static string CommandParser(string rawCommand)
{
if (rawCommand != "")
{
// Removes the '/' from the string and split it to an array of words.
string[] formattedCommand = rawCommand.Remove(0, 1).Split(" ");
try
{
// Finds the command and runs the appropiate function.
return $"Output: {validCommands[formattedCommand[0]].Invoke()}";
}
catch (KeyNotFoundException)
{
// If command isn't found this will be printed.
return $"Invalid command \"{rawCommand}\".";
}
}
else
{
// Returns an empty string to allow for new lines.
return "";
}
}

public static string TestMethod()
{
return "Yay it worked";
}
}
}
My question is, how do I modify this code so I can add parameters to TestMethod and still call it in this way. Or even if there is a better/cleaner way to build something like this I'll look into what ever information I'm provided. Thank you so much for any and all assistance.
7 replies
CC#
Created by 𝒲𝒾𝒢𝓏𝒢𝓇𝓇 on 9/6/2023 in #help
βœ… No SKD's?
Hello I am trying to use VSCode to code and I have installed DotNet 7.0.400 and I have downloaded the SKD's all in default locations. So, why is VSCode still telling me I don't have SDK's? Any Suggestions?
28 replies
CC#
Created by 𝒲𝒾𝒢𝓏𝒢𝓇𝓇 on 9/3/2023 in #help
❔ C# extension is still Brokey
Hello helpers of the C# discord. So, a few weeks ago there was a bug in the C# Extension of VSCode. I believe that bug is now fixed, as there has been an update. But now it's telling me I have no SDK's installed. I have installed under the default path and the "dotnet --list-sdks" shows I have 7.0.400. What can I do to fix this issue and get the extension to work again?
2 replies
CC#
Created by 𝒲𝒾𝒢𝓏𝒢𝓇𝓇 on 8/23/2023 in #help
❔ OmniSharp is Brokey
I updated the C# extension on VS Code and it told me I needed to update my C# to get OnmiSharp to work. I updated it to the latest version I could find, that being 7.0.10 but OmniSharp is asking for 7.0.100 and thus won't work. What can I do to fix this?
8 replies
CC#
Created by 𝒲𝒾𝒢𝓏𝒢𝓇𝓇 on 8/15/2023 in #help
❔ CMD Causing Problems with Custom Input Class
I am working on a C# project that takes input from the user from CMD, and because I like my software to run a specific way I made my own Input Class. It adds a few things that the normal Console.ReadLine() does not include, like command histories and such. On Mac it runs perfectly, but I have not gotten it to work on windows because of a bothersome issue. With CMD while using the Console.ReadKey() and looping over it like this:
string input = "";
while (true)
{
input += Console.ReadKey().Key.ToString();
}
string input = "";
while (true)
{
input += Console.ReadKey().Key.ToString();
}
When you press backspace it moves the cursor over to the left one. I have noticed a few small issues like this, making getting CMD support difficult. My question is, is there a way to disable these shortcuts and just get the key input?
11 replies