Gets all switch cases [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm writing a console application with a switch that contains a lot of commands, and the command called "help" that could output all cases in the switch without have to write them all.
switch(Console.ReadLine())
{
case "help":
Console.WriteLine("All switch cases");
break;
case "command1":
// Code
break;
}

I would sugest using a dictionary, for example:
var dict = new Dictionary<string, Action>
{
{ "help", doHelp },
{ "command1", doCommand1 }
};
// then you can do:
var action = dict["help"];
action();
// if you want to add another command later, use:
dict.Add("command2", () => doCommand2());

Related

Fill Enumerable Inside Enumerable in .NET 5 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a "Panel" model that has multiple "Panel Pages". I would like to get a list of all panels, and fill each panel with its respective "Panel Pages".
Here is my code currently (that works):
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
var customPanels = _customPanelService.GetDynamicCustomPanels();
var dynamicCustomPanels = customPanels.ToList();
foreach (var customPanel in dynamicCustomPanels.ToList())
{
var customPanelPages = _customPanelPageService.GetCustomPanelPages(customPanel.PanelGUID.ToString());
customPanel.CustomPanelPages = customPanelPages;
}
return dynamicCustomPanels;
}
How do I do this in an minimal amount of lines?
This should work:
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
return _customPanelService.GetDynamicCustomPanels().Select(p => {
p.CustomPanelPages = _customPanelPageService.GetCustomPanelPages(p.PanelGUID.ToString());
return p;
});
}
That's technically 3 statements (two returns and an assignment) and one block, though it's kind of an abuse of the Select() method. I might write it like this instead:
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
foreach(var p in _customPanelService.GetDynamicCustomPanels())
{
p.CustomPanelPages = _customPanelPageService.GetCustomPanelPages(p.PanelGUID.ToString());
yield return p;
}
}
Which is... also 3 statements (counting foreach) and one block, just spaced different to use one more line of text.

Why isn't my if statement working inside of my switch statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to make a program that grades student tests. I'm trying to find a way to use loops and not write out a lot of variables. I thought I could use a switch statement then put an if statement inside of it.
For example, in the loop, if the student's answer for question one equals c then add 1 to the counter, but for some reason my code doesn't work.
int Counter = 0;
string StudAns;
for (int i = 1; i <= 10; i++)
{
Console.Write("Students answer for question {0}: ");
StudAns = Console.ReadLine();
switch (i)
{
case '1':
if (StudAns == "c")
{
Counter++;
}
default:
break;
}
}
There are many problems with your code, but answering the question itself, i is a number, not a character. Change your switch case to the following instead:
case 1: // not '1'
That will highlight the next problem, you're missing a break; before your default case.

How to pass or treat a string class name as class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have the following code where className is the string value that I need to pass as a class so that I can avoid writing multiple switch-case statements.
[When(#"Execute Request with model ""(.*)""")]
public void WhenExecuteRequestWithModel(string className)
{
switch (className)
{
case "Customer":
if (_settings.Request.Method == Method.GET)
_settings.Response = _settings.RestClient.Execute<Customer>(_settings.Request);
else
_settings.Response = _settings.RestClient.ExecuteAsyncRequest<Customer>(_settings.Request).GetAwaiter().GetResult();
break;
case "CustomerStatus":
if (_settings.Request.Method == Method.GET)
_settings.Response = _settings.RestClient.Execute<CustomerStatus>(_settings.Request);
else
_settings.Response = _settings.RestClient.ExecuteAsyncRequest<CustomerStatus>(_settings.Request).GetAwaiter().GetResult();
break;
}
}
Please help
It looks like you're trying to bind this with Specflow, and so you can only pass in a string. If the passed className were the full name of the class, or if you are able to deduce the class's full name (by prepending a namespace, for example) then you should be able to use Reflection to instantiate the appropriate generic method.
But, frankly, you might be better off just sticking with a switch statement. Either way, you should be able to reduce repetitive code by creating a helper method.
switch (className)
{
case "Customer":
ExecuteRestMethod<Customer>()
break;
case "CustomerStatus":
ExecuteRestMethod<CustomerStatus>();
break;
}
public void ExecuteRestMethod<T>()
{
if (_settings.Request.Method == Method.GET)
_settings.Response = _settings.RestClient.Execute<T>(_settings.Request);
else
_settings.Response = _settings.RestClient.ExecuteAsyncRequest<T>(_settings.Request).GetAwaiter().GetResult();
}

C# switch case Array contains string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is it possible to look up if a string in contained in an Array, with a switch case?
string text = "blalbac";
string arr[] = {"a","b","c"};
switch (text)
{
case arr.Contains(filename):
//do..
break;
}
I’m not sure what you’re trying to do. Do you want something like
foreach(string item in arr)
{
if(text.Contains(item))
{
...
}
}

Concatenate elements of a list in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wanted to try some examples in C# using collections and generics but I am stuck at this example.
This is my code in c#,
public List<string> CurrentCount(int week, int year)
{
List<string> lst = new List<string>();
lst.Add("current:");
lst.Add("10");
lst.Add("target:");
lst.Add("15");
return lst;
}
It gives me result like this :
["current:","10","target:","15"]
But I want it like this :
["current": 10,"target": 15] or
["current": "10","target": "15"]
Any ideas will be helpful.
Thanks.
You want a Dictionary, not a List.
var dict = new Dictionary<string, int>();
dict.Add("current", 10);
dict.Add("target", 15);

Categories

Resources