dynamic - in c#, how to call a function according to db value without if/else -


i trying call function according db value without using if/else or case. sample code below , think explain better.

logoobj.data ms = logoapp.newobj(logoobj.dataobjtype.item); // item 

the "item" information comes select query. "item" "customer" well. therefore, dont want write code below.

if(datatable_result.rows[0][0].tostring()=="item")    ms = logoapp.newobj(logoobj.dataobjtype.item); else if(datatable_result.rows[0][0].tostring()=="customer")    ms = logoapp.newobj(logoobj.dataobjtype.customer); ......... ......... ......... 

can have piece of below, because need

logoobj.data ms = logoapp.newobj(logoobj.dataobjtype.xxxxxdb_valuexxxxx); 

you can use enum.parse:

var enumvalue = enum.parse(typeof(logoobj.dataobjtype), datatable_result.rows[0][0].tostring(), true); //true = ignore case ms = logoapp.newobj(enumvalue); 

if have potential items in database not match of enum values, should instead use enum.tryparse , check ensure has correctly parsed value before calling newobj.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -