Class type as function argument in vb.net -
hi using linqtocsv load data different files variables.
i have 3 different files , using classes import these want make code more generic , struggling when comes passing in type argument.
i using code similar following:
public class csv1 property afield1 string property afield2 string end class public class csv2 property bfield1 string property bfield2 string end class public class csv3 property cfield1 string property cfield2 string end class public module csvtools private function getcsvdata(of t)(fname string) ienumerable(of t) if io.file.exists(fname) dim cf = new csvfiledescription { .separatorchar = ",", .firstlinehascolumnnames = true, .ignoreunknowncolumns = true } dim cc = new csvcontext() return cc.read(of t)(fname, cf) 'this line above gives compile error: ' type parameter 't' must have either 'new' constraint or 'structure' constraint satisfy 'new' ' constraint type parameter 't' end if end function property file1 ienumerable(of csv1) property file2 ienumerable(of csv2) property file3 ienumerable(of csv3) public sub loaddata() file1 = getcsvdata(of csv1)("file1.csv") file2 = getcsvdata(of csv2)("file2.csv") file3 = getcsvdata(of csv3)("file3.csv") end sub end module
anyone care point out doing wrong? don't want replicate getcsvdata code each different type of file want import..
i have googled etc answer , have tried throws different error. think brain melting bit can point me in right direction? thanks
cc.read(of t)
requires t
have default constructor, did not ensure. add constraint method declaration:
private function getcsvdata(of t {class, new})(fname string) ienumerable(of t)
Comments
Post a Comment