how to use if with string in Settings c# [duplicate] - c#

This question already has answers here:
Operator '==' cannot be applied to operands of type 'string' and 'int' [closed]
(2 answers)
Closed 4 years ago.
if (Properties.Settings.Default.BG == 1)
{
}
I think there is something I need to write before (1) but I don't know

To represent a string, you need to surround the value with double quotes. Currently you're comparing it to an integer. Instead, do something like this:
if (Properties.Settings.Default.BG == "1")
{
}
Another way to do this would be to call the built-in ToString() method that belongs to all objects:
if (Properties.Settings.Default.BG == 1.ToString())
{
}

Related

Ternary expression cannot choose between two method groups [duplicate]

This question already has answers here:
Conditional operator and Comparison Delegate
(2 answers)
Conditional statement, generic delegate unnecessary cast
(4 answers)
Why doesn't the C# ternary operator work with delegates?
(2 answers)
How can I assign a Func<> conditionally between lambdas using the conditional ternary operator?
(4 answers)
Closed 4 years ago.
I have a choice between two statically defined converters, and I want to use a ternary expression to make the choice:
Func<Input, Output> converter = useConverter1
? MyConverter.Converter1
: MyConverter.Converter2;
The compiler will not accept this expression. It fails with a message saying "There is no implicit conversion between 'method group' and 'method group'".
The structure of MyConverteris
public static class MyConverter
{
public static Output Converter1(Input input)
{
....
}
public static Output Converter1(Input input)
{
....
}
}
Can I work around this without an if-else statement?

Cannot convert type System.Collections.Generic.IEnumerable<> to bool [duplicate]

This question already has answers here:
Cannot implicitly convert type System.Collections.Generic.IEnumerable<> to bool
(2 answers)
Closed 6 years ago.
I'm trying to code this in an if condition:
if (submitAnswersResponseRootObject.Response.SubmitAnswersResult.Prompts.prompt.Where(p=>p.code == 7101))
but I'm getting error:
Cannot implicitly convert type
System.Collections.Generic.IEnumerable<> to bool
What am I doing wrong?
Maybe Any instead of Where that returns other collection (filtered)?
if (submitAnswersResponseRootObject.Response.SubmitAnswersResult.Prompts.prompt.Any(p=>p.code == 7101))
Maybe like this:
if(submitAnswersResponseRootObject.Response.SubmitAnswersResult.Prompts.
prompt.Where(p=>p.code == 7101).Any())

Creating a custom operator - '!?' (negation of '??'). Is it possible? [duplicate]

This question already has answers here:
Is it possible to create a new operator in c#?
(7 answers)
Closed 8 years ago.
Is it possible to create a custom operator like '!?' (negation of '??') instead of writing long expression:
int? value = 1;
var newValue = value != null ? 5 : (int?)null;
I want to have:
var newValue = value !? 5;
Thanks!
No.
You cannot create your own operators in C#. You can only override (some of the) ones that already exist in the language.
You cannot define new operator in C# - the only way is to redefine existing ones, but you can not redefine ?: and ?? operators.

string to Enum in c# [duplicate]

This question already has answers here:
Search for a string in Enum and return the Enum
(13 answers)
Closed 9 years ago.
I need to assign a string to a Enum value. My scenario and code below
Am accessing a webservice method say addWhere(int i ,int j,Comparison)
Where Comparison is of type Enum.
Am fetching value from UI and having it in a string
string strComparison= "AND"; or
string strComparison= "OR"
i need to set these values to ENUM. I tried the below code
addWhere(2 ,3,Enum.Parse(typeof(Comparison),strComparison))
But didnt worked. What is the way to solve this or any alternate methods ?
Thanks
Looks like your missing the return cast i.e.
(Comparison)Enum.Parse(typeof(Comparison), strComparison);
Enum.Parse returns an object were as your addWhere method is expecting a Comparison type value.
You must cast the result of Enum.Parse to the correct type:
addWhere(2, 3, (Comparison)Enum.Parse(typeof(Comparison), strComparison));
You can do something like:
Comparison comparison;
if(Comparison.TryParse(strComparison, out comparison))
{
// Work with the converted Comparison
}
As said in earlier posts, you might be missing type casting.
enum Comparision
{
AND,
OR
}
class Program
{
static void Main(string[] args)
{
Comparision cmp = (Comparision)Enum.Parse(typeof (Comparision), "And", true);
Console.WriteLine(cmp == Comparision.OR );
Console.WriteLine(cmp == Comparision.AND);
}
}

What does the tilde do in C#? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the tilde before a function name mean in C#?
What is the tilde (~) in the enum definition?
I know "~" is for Finalzier methods but now I saw some C# code like this:
if (~IsFieldDeleted(oRptField.GetLayoutField()) != 0)
{
oCollection.Add(oRptField, oRptField.ObjectKeyString);
// some more stuff
}
notice that "~" in the first line?
and then if I go to implementation of IsFieldDeleted it is a method that returns an int.
private int IsFieldDeleted(LayoutLib.LayoutField oLayoutField)
{
Collection oColl = GetFieldIdsForField(oLayoutField);
return (oColl.Count == 0) ? 1 : 0;
}
The ~ operator performs a bitwise complement.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-complement-operator
IsFieldDeleted() returns an int, which is a type to which that operator can be applied (int, uint, long, ulong). The bitwise complement is taken and then compared to zero.
I don't see how the if(...) can ever be true, since IsFieldDeleted() only returns 0 or 1 and ~0 and ~1 are both not zero.

Categories

Resources