VB.NET CType: How do I use CType to change an object variable "obj" to my custom class that I reference using a string variable like obj.GetType.Name? -
the code below works class hard coded "xccustomers" in retrieveidandname method use ctype. however, able pass in various classes , property names integer , string list returned. example, in code below, pass in "xcemployees" retrieveidandname method. feel close... hoping knew how use ctype can pass in class name string variable.
note, other examples have seen , tried fail because using option strict on disallows late binding. why need use ctype. studied "activator.createinstance" code examples try class reference instance string name unable ctype work that.
when use obj.gettype.name or obj.gettype.fullname in place of "xccustomers" in ctype(obj, xccustomers)(i) error "type 'obj.gettype.name' not defined" or "type 'obj.gettype.fullname' not defined"
thanks help.
rick
'+++++++++++++++++++++++++++++++ imports datalaasxc.business imports datalaasxc.utilities public class uccustomerlist 'here calling method: public sub callingsub() dim customerlist new xccustomers() dim customeridandname new list(of xccustomer) = retrieveidandname(customerlist, "customerid", " customername") 'this code below fails because had hard code “xccustomer” in “dim item...” section of retrieveemployeesidandname method. dim employeelist new xcemployees() dim employeeidandname new list(of xcemployee) = retrieveidandname(employeelist, "employeeid", " employeename") 'doing stuff here... end sub 'here method use class name string when use ctype: private function retrieveidandname(byval obj object, byval idpropname string, byval namepropname string) list(of intstringpair) dim selecteditems list(of intstringpair) = new list(of intstringpair) dim fullyqualifiedclassname string = obj.gettype.fullname dim count integer = cint(obj.gettype().getproperty("count").getvalue(obj, nothing)) if (count > 0) integer = 0 count - 1 'rather hard coding “xccustomer” below, want use “obj.gettype.name”??? dim item intstringpair = new intstringpair(cint(ctype(obj, xccustomers)(i).gettype().getproperty("customerid").getvalue(ctype(obj, xccustomers)(i), nothing)), _ cstr(ctype(obj, xccustomers)(i).gettype().getproperty("customername").getvalue(ctype(obj, xccustomers)(i), nothing))) selecteditems.add(item) next end if return selecteditems end function end class
'+++++++++++++++++++++++++++++++
' below supporting classes if need see else happening:
namespace datalaasxc.utilities public class intstringpair public sub new(byval _key integer, byval _value string) value = _value key = _key end sub public property value string public property key integer end class end namespace '+++++++++++++++++++++++++++++++ namespace datalaasxc.business public class xccustomer public property customerid integer public property customername string end class end namespace '+++++++++++++++++++++++++++++++ namespace datalaasxc.business public class xccustomers inherits list(of xccustomer) public sub new() populatecustomersfromdatabase() end sub public sub new(byval getempty boolean) end sub end class end namespace '+++++++++++++++++++++++++++++++ namespace datalaasxc.business public class xcemployee public property employeeid integer public property employeename string end class end namespace '+++++++++++++++++++++++++++++++ namespace datalaasxc.business public class xcemployees inherits list(of xcemployee) public sub new() populateemployeesfromdatabase() end sub public sub new(byval getempty boolean) end sub end class end namespace
since list(of t) implements non-generic ilist interface, change function declaration to:
private function retrieveidandname(byval obj system.collections.ilist, byval idpropname string, byval namepropname string) list(of intstringpair)
and troublesome line become (with using property name parameters):
dim item intstringpair = new intstringpair(cint(obj(i).gettype().getproperty(idpropname).getvalue(obj(i), nothing)), _ cstr(obj(i).gettype().getproperty(namepropname).getvalue(obj(i), nothing)))
of course, still have first parameter object, , attempt cast ilist, that's you.
Comments
Post a Comment