Guapo
Guapo

Reputation: 3482

String to Enum fails if not written exactly even with ignorecase set to true?

if (user.Access == (UserType)Enum.Parse(typeof(UserType), str, true))

I have a channel bot that takes commands as follow:

.add username usertype

Now I have to check if the username already have that written access or not but when the usertype is not exactly written as the Enum it always fail.

HeadAdmin written as headadmin for example result in the follow error:

System.NullReferenceException: Object reference not set to an instance of an object.

Also if I type a non existent enum it will tell me that value X was not found.

How do I convert usertype so it will return true if match and false if not match or not exist ?

Upvotes: 0

Views: 1029

Answers (1)

Toby
Toby

Reputation: 7554

Given your example code and the stated error, my first assumption would be that the user object is null.

Upvotes: 4

Related Questions