Casting a value from a table to an Enum using Enum.ToObject

  • Thread starter Thread starter KyleK
  • Start date Start date
K

KyleK

I am having problems using the Enum.ToObject method.

this.ToFile = Enum.ToObject(typeof(LogLevel),(int)(row[TOFILE_FIELDNAME]));

does not work. it gives me

"<myfile>.cs(146): Cannot implicitly convert type 'object' to
'DistGISLogging.LogLevel'

on compile. i thought the whole point of the ToObject method was to
create an object of that type of enumeration?

this.ToFile is defined

LogLevel ToFile{ get{}set{} }

any help appreciated!

-k
 
KyleK said:
I am having problems using the Enum.ToObject method.

this.ToFile = Enum.ToObject(typeof(LogLevel),(int)(row[TOFILE_FIELDNAME]));

does not work. it gives me

"<myfile>.cs(146): Cannot implicitly convert type 'object' to
'DistGISLogging.LogLevel'

on compile. i thought the whole point of the ToObject method was to
create an object of that type of enumeration?

Yes - but the compiler only knows that it returns something of type
object.
this.ToFile is defined

LogLevel ToFile{ get{}set{} }

any help appreciated!

You don't actually need Enum.ToObject as far as I can see. Just use:

this.ToFile = (LogLevel)(int)row[TOFILE_FIELDNAME];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top