Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToEnumOrDefault #61

Closed
kurnakovv opened this issue May 26, 2024 · 7 comments · Fixed by #67
Closed

ToEnumOrDefault #61

kurnakovv opened this issue May 26, 2024 · 7 comments · Fixed by #67
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@kurnakovv
Copy link
Owner

kurnakovv commented May 26, 2024

ToEnumOrDefault

Description

Try convert string value to enum or return default value if not possible.

Signature

bool ToEnumOrDefault(this string, bool [default = true], TEnum [default = TEnum]) where TEnum : struct, Enum

Type Parameters

TEnum - Enum that we want try to get after convert.

Parameters

  • string stringValue - Input string we want to convert to TEnum.
  • bool ignoreCase - Ignore or regard case.
  • TEnum defaultValue - TEnum or default of TEnum.

Returns
TEnum or defaultValue if not possible to convert.

Code examples

public enum MyEnum { First, Second, Third }
string stringValueFirst = "First";
string stringValueSecond = "Second";
string stringValueInvalid = "Blablabla";

MyEnum enumFirst = stringValueFirst.ToEnumOrDefault<MyEnum>();
MyEnum enumSecond = stringValueSecond.ToEnumOrDefault<MyEnum>(false);
MyEnum enumThird = stringValueInvalid.ToEnumOrDefault<MyEnum>(MyEnum.Third);

// output
// enumFirst - MyEnum.First
// enumSecond - MyEnum.Second
// enumThird - MyEnum.Third
@kurnakovv kurnakovv added the enhancement New feature or request label May 26, 2024
@kurnakovv kurnakovv modified the milestones: 2.4, 2.4.0 May 26, 2024
@kurnakovv kurnakovv changed the title TryToEnumOrDefault ToEnumOrDefault Jun 28, 2024
@Kurnakova
Copy link
Contributor

Hi

@Kurnakova
Copy link
Contributor

@kurnakovv Hey, why do we sometimes use TAnotherEnum and sometimes TEnum, what's the difference, why do we need two?

@kurnakovv
Copy link
Owner Author

kurnakovv commented Aug 1, 2024

@Kurnakova it depends on an input parameter
TAnotherEnum - when you convert input enum to another enum
TEnum - when you convert input string to enum
In your cases you need to use TEnum, because input parameter is string (not enum)

I hope it's clear

P.S. Of course you can use TEnum for both cases, or just T, but I think my code is better

@Kurnakova
Copy link
Contributor

Kurnakova commented Aug 1, 2024

@kurnakovv

Returns
"TAnotherEnum" or "defaultValue" if not possible to convert.

Why does say that it returns TAnotherEnum though? Or is this a typo?

@kurnakovv
Copy link
Owner Author

Good catch, fixed

@Kurnakova
Copy link
Contributor

@kurnakovv

Code Examples
bool enumFirst = stringValueFirst.ToEnumOrDefault();

Why is it bool? Shouldn't it be "MyEnum" or smth like that, since this method returns enums?

@kurnakovv
Copy link
Owner Author

@Kurnakova yep, good catch, thank you! I fixed

@kurnakovv kurnakovv linked a pull request Sep 1, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants