This question already has answers here:
Validate Enum Values
(12 answers)
Closed 6 years ago.
If I create an enum like this
public enum ImportType
{
Direct,
Indirect,
InBond
}
and I have a method that takes an ImportType as parameter as follows
public bool ProcessValidImport(ImportType type)
{
// Process ImportType variable here
}
I can go and call the method as follows
bool blnProcessed = ProcessValidImport((ImportType)7);
But the ImportType variable value of 7 passed in to the method is not valid at all because any integer will work if cast. The enum defaults to an int type, so what is the best way to validate that an enum in this case is in fact a valid ImportType.
I don't know if I understood you correctly but you can validate enum easily using:
int value = 7;
bool isDefined = Enum.IsDefined(typeof (ImportType), value);
Related
This question already has answers here:
Easiest way to extend a Struct (PointF)
(3 answers)
Extension methods versus inheritance
(9 answers)
Extend an existing struct in C# to add operators
(7 answers)
A way to extend existing class without creating new class in c#
(3 answers)
Closed 1 year ago.
Is it possible to add methods into variables, example being.
public static int BoolToInt(bool entry)
{
if(entry == true) { return 1; }
else { return 0; }
}
bool example = checkbox1.Checked;
RandomMethod(example.BoolToInt());
Looking into minimizing visual clutter and help readability (I find RandomMethod(example.BoolToInt(), example2.BoolToInt()); easier to read than RandomMethod(BoolToInt(example), BoolToInt(example2));) I was wondering if this was possible, upon research I found this Can you assign a function to a variable in C#? which feels like it's the right direction, but it makes the variable become the method, when I want to just add into it. I'm a newbie so I couldn't go from there to what I want nor know if it's theres a way to do it, also couldn't find much reading the Microsoft Docs.
You can use C# extension methods to "extend" exists types by declaring new methods for the extended type.
static class BoolExtensions {
public static int ToInt(this bool value) {
return value ? 1 : 0;
}
}
Then you can use as:
var example = true;
var exampleAsInt = example.ToInt();
Reference:
Extension methods
This question already has answers here:
Simply check for multiple statements C#
(2 answers)
C# - Prettier way to compare one value against multiple values in a single line of code [duplicate]
(2 answers)
Closed 3 years ago.
I have got method:
private bool MyMethod(PlantType plantType)
{
return plantType.PlantMoveType == PlantMoveType.PlantReady
|| plantType.PlantMoveType == PlantMoveType.PlantRelase
}
Can I write it into other way? Maybe with LINQ?
One way is to put the enum values that you want to check against into an array, and call Contains.
return new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
.Contains(plantType.PlantMoveType);
If you are using C# 7 or later, you can also write the method as expression-bodied:
private bool MyMethod(PlantType plantType) =>
new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
.Contains(plantType.PlantMoveType);
Well a small simplification would be to pass the type (enum?) of the property PlantMoveType instead of PlantType as the parameter.
Beyond that, you could declare the types to check for as e.g. an array. In case you'd like to reuse that array, you can also declare it outside the scope of the method:
private static PlantMoveType[] _plantStates =
new []{PlantMoveType.PlantReady, PlantMoveType.PlantRelase};
private bool MyMethod(PlantMoveType plantMoveType)
{
return _plantStates.Contains(plantMoveType);
}
This question already has answers here:
How can I convert String to Int?
(31 answers)
Closed 3 years ago.
I can't get the string of text_box.text to convert to an int then compare to another int.
I also need to check to see if the string of text_box.text can be turned into an int.
I've tried .ToInt32, which I have always used and it has been fine. I have no idea how to test if the string of text_box can be turned into an int.
public static void before (int bS)
{
beforeScore = bS;
}
//some space later
if (score_bet_text_box.Text.ToInt32() > beforeScore)
{
MessageBox.Show("You can't bet more than you have", "game");
}
I expect it to convert the string of text_box into an int, then compare it to the other int.
I also hope to test if it can be converted into an int, but have no clue how so it is not shown in the code above.
ToInt32 isn't a method on a string unless you have an extension method somewhere. You want to use the TryParse method as follows...
if(int.TryParse(score_bet_text_box.Text, out int result))
{
if(result > beforeScore)
{
MessageBox.Show("You can't bet more than you have", "game");
}
}
If you are using an older version of C# you'll have to define result outside the if as follows:
int result;
if(int.TryParse(score_bet_text_box.Text, out result))
This question already has answers here:
How do I cast int to enum in C#?
(32 answers)
Convert String to Type in C# [duplicate]
(4 answers)
Enum String Name from Value
(14 answers)
Get the name of Enum value
(3 answers)
Get enum name when value is known
(7 answers)
Closed 5 years ago.
From another layer in an application I can get back the name of the enum type and an integer value. How can I use this data to get the string representation of that value for that enum.
For example, if I have an enum:
public enum Cheese
{
Edam = 1,
Cheddar = 3,
Brie = 201,
RedLeicester = 1001
}
and the data I have available is
string dataType = "Cheese";
int dataValue = 3;
How can I get the result:
"Cheddar"
In addition to the name of enum itself you should know namespace and assembly that enum is declared at. Assuming you run code below in assembly where enum is defined and all your enums are defined in the same namespace you know at compile time, you can do it like that:
string dataType = "Cheese";
int dataValue = 3;
var enumType = Type.GetType("Namespaces.Of.Your.Enums." + dataType);
var name = Enum.GetName(enumType, dataValue);
If your enum is in another assembly than this code is running at - you will need to provide assembly qualified name of your enum type (again with namespace).
Type t = Type.GetType(dataType)
string value = Enum.GetName(t, dataValue)
should do the trick (uncompiled, on subway)
enum.Parse() would be your friend here.
Like so:
int cheeseType = 1;
(Cheese)Enum.Parse(typeof(Cheese), cheeseType.ToString());
This question already has answers here:
How to compare enum and int values?
(7 answers)
What's the simplest way to compare an enum to a integer value return from a DB
(5 answers)
C# Enum - How to Compare Value
(5 answers)
Closed 5 years ago.
I have a simple enum with a few values
Enum Status{
Available,
Taken,
Sold//and many more
}
I want to check whether the status is somewhere in between the possible values(assuming the values are written in such an order that the lowest are the beginning of a process, and the last ones are the final steps of the process)
Something like
Status s1;//Some value
if(s1<5 && s1>3)
//The value is one of the enum values in those range
//(for example the job has already been accepted, but has still not been
//shipped)
Is it possible?
Thanks.
You can assign integer values to your enum and then check.
enum Status
{
Available = 1,
Taken = 2,
Sold = 3
//and many more
}
Status s1; // any value
if ((int)s1 <= 5 && (int)s1 >= 3)