Sometime we need to convert string to an enum type, say I need to store my enum value into viewstate then we cannot store enum or any object to viewstate so converted that to string and store it, and on postback need to read the viewstate and convert to enum, so how to convert a string into enum? We will see this in this article.
Let’s say our enum is like this:
Public enum ReportType
{
DailyReport = 1,
WeeklyReport = 2,
MonthlyReport = 3,
QuarterlyReport = 4,
YearlyReport = 5
}
Let’s create object of our enum type and assign a value and store into viewstate
ReportType type = new ReportType();
type = ReportType. MonthlyReport;
ViewState[“reporttype”] = type.ToString();
Now we need to read and convert it into our above enum type
String reportType = Convert.ToString(ViewState[“reporttype”]);
//Now convert it to enum type
ReportType type = new ReportType();
type = (ReportType)Enum.Parse(typeof(ReportType), reportType, true);
We converted out viewstate value to our desired enum type.
![]() |
Process manager with a reputed organization, Fond of learning new features and technology related to C#, ASP.Net, SQL Server, MVC etc.I like to help others, if I can
|
By Hamden On 21 May, 13 Viewed: 1,553 |
As we know the benefit of constant and enum in any other language, we need to it wherever need in our project. But angularjs does not have any enum concept, we will try to create some constants, object to achieve the same goal of enum easily with example. Luckily AngularJS provides a `constant`... By Ali Adravi On 02 Sep 2015 Viewed: 49,116
In MVC we have EnumDropDownListFor which we can directly bind with our any enum, it is very similar to DropDownListFor. In this article we will try to create an enum and use it into our model and see how we can bind and save it into the database. As we know we cannot keep space in the name of emum... By Ali Adravi On 15 May 2015 Viewed: 27,004
To Bind a DropDownListFor or DropDown in asp.net MVC from database, from enum or from hard coded values, most people suggest to add a property in the model with IEnumarable or IList. We can use it but not very helpful because after every postback we will need to fill the list again otherwise it... By Ali Adravi On 04 Apr 2015 Viewed: 47,290
In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi On 27 Mar 2013 Viewed: 2,615
An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden On 27 Mar 2013 Viewed: 4,571