arrays - Portable Mode Function in Visual Basic -
i'm trying make mode
function accept arrays of type, , i'm not making progress. here's i've got now:
private function mode(byref list object) object dim array() object = {} try array = ctype(list, object()) catch ex exception messagebox.show("failed cast array of objects in mode function!") return nothing end try dim uniqueobjects() integer = {array(0)} dim frequency() integer = {1} integer = 0 array.length - 1 j integer = 0 uniqueobjects.length - 1 'loop through frequency if array(i) = uniqueobjects(j) frequency(j) += 1 exit elseif j = uniqueobjects.length - 1 redim preserve uniqueobjects(uniqueobjects.length) 'add unique objects array uniqueobjects(uniqueobjects.length - 1) = array(i) redim preserve frequency(frequency.length) 'increment frequency frequency(frequency.length - 1) += 1 end if next next return uniqueobjects(system.array.indexof(frequency, frequency.max)) end function
i rid of slow call ctype
, pass array of objects function, gives me weird error when pass array of integers function:
error 1 value of type '1-dimensional array of integer' cannot converted '1-dimensional array of object' because 'integer' not reference type. {filename}.vb {line} {column} {project name}
this turning out trickier had expected. can offer advice?
how making generic function?
private function mode(of t)(byref array t()) object '... end function
or
private function mode(of t)(byref array t()) t() '... end function
then do:
dim obj object = mode(of integer)({0, 1, 2, 3})
or:
dim obj integer() = mode(of integer)({0, 1, 2, 3})
Comments
Post a Comment