C# Convert string to name of bool variable [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 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.
}

Related

Does this public get set for a private variable make sense? [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 am working on a codebase a student with questionable knowledge made, here I am just confused if there is any reasonable intend there or if its just unnecessary... I personally would just use the ShowNewButton field and trash the _showNewBtn, please provide an opinion.
private _showNewBtn;
public bool ShowNewButton
{
get => _showNewBtn;
set
{
bbNew.Visibility = value == false ? BarItemVisibility.Never : BarItemVisibility.Always;
_showNewBtn = value;
}
}
I see no reason (in the sample you provided) for the _showNewBtn.
You now have 2 markers which contain the visibility state of the button. At some point, this will cause problems if you aren't carefull.
Either remove the _showNewBtn completely:
public bool ShowNewButton
{
get => bbNew.Visibility == BarItemVisibility.Always;
set
{
bbNew.Visibility = value == false ? BarItemVisibility.Never : BarItemVisibility.Always;
}
}
Or justify the existance of _showNewBtn which makes my remark null and void.

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);

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))
{
...
}
}

Define if statement for property 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 4 years ago.
Improve this question
public class hamid
{
private int[] arr = new int[10];
public int[] Arr
{
get => arr;
set
{
if (value < 0)
Environment.Exit(1);
else arr = value;
}
} // If in error.
}
I want have a if statement. For example if (values < 0)
But I have an error, please help me.
value is an array of integer, it can't be zero. Any element in the array could be zero. If you are trying to determine if the array is null, then you could change the check to be:
if (value == null)
{
Environment.Exit(1);
}

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