c# - Cannot convert string to enum -


i need make adapter class.

trip.resource enum named modalityresource.

transportordertrip.modalityresource string.

if(enum.isdefined(typeof(modalityresource), transportordertrip.modalityresource))     trip.resource = enum.parse(typeof(modalityresource), transportordertrip.modalityresource, true); 

possible values modalityresource here:

 public enum modalityresource { none, chassis, trailer } 

the error one: cannot implicitly convert type 'object' 'modalityresource'. might know wrong?

edit: never mind, bit confused.

  trip.resource = (modalityresource) enum.parse(typeof (shared.interfaces.modalityresource), transportordertrip.modalityresource, true); 

you've cast result of enum.parse modalityresource. in .net 4 or newer can use generic overloads of function.

trip.resource = (modalityresource)enum.parse(typeof(modalityresource), transportordertrip.modalityresource, true); 

see also

http://msdn.microsoft.com/en-us/library/dd783499.aspx


Comments