C# switch case Array contains string [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 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))
{
...
}
}

Related

Search Array and get substring result [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a string array as follows:
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
In essence, it is broken down by id|active where for example id is say 1122 and active is true or false.
Say my id was 1123, how would I search this array to get the value of true in this case? I understand Substring needs to be used with IndexOf but not sure how to tie it together.
Little bit of LINQ and String.Split should do the trick.
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
int id = 1123;
var itemWithGivenId = stringArray
.SingleOrDefault(s => int.Parse(s.Split('|')[0]) == id);
Console.WriteLine(bool.Parse(itemWithGivenId.Split('|')[1]));

How to convert Span<byte> to ASCII 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 3 years ago.
Improve this question
I can convert byte array to ASCII string in C# by Encoding.ASCII.Getstring() method.
But I don't know how to convert Span to String.
Added I want to use Span<byte>.ToArray().
Encoding.GetString does not accept Span<byte>.
But you can create a Extension Method:
public static class EncodingExtensions
{
public static string GetString(this Encoding encoding, Span<byte> source)
{
//naive way using ToArray, but possible to improve when needed
return encoding.GetString(source.ToArray());
}
}
Then you are able to call:
var foo = new Span<byte>();
var bar = Encoding.ASCII.GetString(foo);

Gets all switch cases [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 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());

C# Convert string to name of bool variable [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 7 years ago.
Improve this question
I trying to make something like that:
// int counter; - this is changing in ther other parts of program
bool abc1; bool abc2; bool abc3; bool abc4;
if("abc" + counter == true)
{
//some code
}
Anyway, I need to convert string and int to bool name. How can I do this?
Use an array instead:
bool[] abc;
// ...
if (abc[counter] == true) {
{
// some code.
}

Cannot apply indexing with [] to an expression of type? [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 have got this error
Error 3 Cannot apply indexing with [] to an expression of type
my problem here
if (client["banhours"] == 0)
{
client["banhours"] = -1;
client["banreason"] = "Infinite time.";
client["banstamp"] = DateTime.Now.AddYears(100);
}
if (Account.State == Database.AccountTable.AccountState.Banned)
{
if (client["banhours"] != -1)
{
DateTime banStamp = client["banstamp"];
if (DateTime.Now > banStamp.AddDays(((int)client["banhours"]) / 24).AddHours(((int)client["banhours"]) % 24))
Account.State = Database.AccountTable.AccountState.Player;
}
}
client is >>>
Client.GameClient client;
Have you tried client.banhours or client.banreason?
If Client.GameClient is a class and those are properties or fields, they must not be accessed like an array or dictionary.

Categories

Resources