Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

My name is rakesh , working as jr.software developer .

I Having an issue , If anybody can solve this please help me.'

I want the string type to be converted as enum type , so that i can use it as enum type in various methods .
Posted
Comments
Rahul Rajat Singh 19-Mar-12 6:51am    
Please elaborate on why you need to do this as the question seems little ambiguous.
raki1111 19-Mar-12 7:07am    
hello rahul , actually , yeah i will say u clear requirement.
i have two enums
for eg:

Public Enum PROOF_TYPE
Address_Proof = 1
PhotoID_Proof = 2
Other_Proof = 3
Photograph = 4
Signature = 5
POA_Signature = 6
End Enum


Public Enum Other_Proof
PAN_Card = 1
Electric_Bill = 2
Telephone_Bill = 3
Gas_Bill = 4
Passport = 5
end Enum

Public Enum Address_Proof
Electric_Bill = 1
Telephone_Bill_of_Land_Line = 2
Gas_Bill = 3
Passport = 4
Voter_ID_Card = 5
Driving_Licence = 6
UIN_Card_Aadhaar = 7
Govt_ID_Card = 8
Bank_Statement_or_Pass_Book = 9
Demat_Transaction_Statement = 10
Registered_Lease_or_Sale_Agreement_of_Residence = 11
End Enum

i am using two checked combo boxes . one is filling the values of Proo_type
and other should fill the values associated with the first combo box.

so now i hv to store it to database with their enums as integer values ,
i want to get the enum value of second checkedcomboboxes.

while passing the string to gettype method its not accepting the string ,it required enum type .
thanks









Try:
VB
Dim today As DayOfWeek = DirectCast([Enum].Parse(GetType(DayOfWeek), "Monday"), DayOfWeek)
 
Share this answer
 
Comments
raki1111 19-Mar-12 7:09am    
i want that DayOfWeek to pass dynamic , according to the checked combo box value.
thanks
OriginalGriff 19-Mar-12 7:32am    
To do that exactly as you want would mean using reflection - the problem is the return type from the DirectCast would have to change.
I would use a Select Case statement to pick which type I was expecting from the drop-down and then use enum.Parse as above.

The problem is that unless you know which enum it is, you don't know what the values mean when you later use it.
raki1111 19-Mar-12 7:10am    
so in that i facing problem in converting the string type to enum type.
raki1111 19-Mar-12 7:44am    
i want something dynamic without using the select case , directly i have to pass the string to gettype method as enum .
Why don't you create a dictionary for each information. The key could be the enum string representations and the value could be the enum value.

That way you can add all the keys from each dictionary into the combo boxes. once the user seelct any value use this you just have to pass the actual enum value as dictionary["selectedstring"] which will be a enum value.

This way you have all the type safety too.

I hope I understood the problem and my suggestion makes sense. if not correct my understanding please.
 
Share this answer
 
Comments
raki1111 19-Mar-12 8:38am    
Thanks for the suggestion rahul .
I tried doing this in another way , and now its working.


Dim obj As Object = ProofType(Val)
Dim enumObj1 As Object = System.Enum.Parse(obj.GetType, m_sName.Replace(" ", "_"))

Private Function ProofType(ByVal iVal As Int32) As [Enum]
Try
Select Case iVal
Case 1
Return New Address_Proof
Case 2
Return New PhotoID_Proof
Case 3
Return New Other_Proof
Case Else
Return Nothing
End Select
Catch ex As Exception
ErrorLog(ex) : Return Nothing
End Try
End Function
This question is not so simple as it may seem. The problem is created by the cases when the different enumeration members are assigned the same integer values. It creates ambiguity which is hard to handle.

I have a comprehensive solution of this problem. It is explained in two of my CodeProject articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

This is the only comprehensive solution I know. Please see. You can find explanation of a number of related techniques (even simpler ones) you could use.

—SA
 
Share this answer
 
Comments
raki1111 20-Mar-12 1:27am    
But can i know that y it will create ambiguity , i hope it will not create ambiguity since i have the correct fields. while saving or retriving from db.(sql server)
Sergey Alexandrovich Kryukov 20-Mar-12 2:02am    
Right. Of course, you can avoid ambiguity with proper selection if member integer values, but there are many cases when the same values are needed for better maintenance. (Those are not fields, they are static members.)
Anyway, I pointed out fully comprehensive solution and explained some techniques in case of ambiguity and without ambiguity and general ways of getting those strings. You are free to use this knowledge the way you want. :-)

If you agree it makes sense, consider accepting this answer formally (green button) -- thanks.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900