Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return FontStyle from string name

Tags:

c#

parsing

fonts

I want to write a function which will return FontStyle and take string as Parameter

FontStyle f = function ("Italic"); // FontStyles.Italic

I don't want to write Switch case or if else statements to do the same.

Can it be done for case insensitive strings?

FontStyle f = function ("italic");
FontStyle f = function ("itAlic"); 

should return same.

like image 972
user270014 Avatar asked May 10 '12 07:05

user270014


1 Answers

In C# it is just an enumeration. So you can convert it like this:

FontStyle f = (FontStyle)Enum.Parse(typeof(FontStyle), "Italic", true);
like image 73
Nick Avatar answered Sep 28 '22 15:09

Nick