Tuesday, 15 April 2014

.net - Ordering a list by an enum -



.net - Ordering a list by an enum -

i'm trying order list enum please see below:

public class animalstoprocess implements icloneable enum animalorder dog cat horse fish end enum public property _animallist list(of animal) public sub new() end sub function clone() animalstoprocess end function private function icloneable_clone() object implements icloneable.clone homecoming clone() end function public sub add(animaltoadd animal) _animallist.add(animaltoadd) end sub public sub getanimals() list(of animal) _animallist() 'this should ordered animalorder enum end sub end class

the add together method called external code passing value , eg:

animals.add(fish) animals.add(horse) animals.add(cat)`

but when getanimals called should homecoming list ordered theenum animalorder.

hence list should contain follwing animals in order cat,horse,fish

thank you.

enums utilize integers internally each value. if don't set them explicitly, they're auto-assigned in order defined in enum, starting 0.

you should able cast string value enum , there integer. (untested)..

public function getanimals() list(of animal) homecoming _animallist.orderby(function(x) cint(directcast(enum.parse(gettype(animalorder), x), animalorder))) end sub

note you're referring value type animal don't have definition of in q. if need access property value match enum, animaltypestring...

public function getanimals() list(of animal) homecoming _animallist.orderby(function(x) cint(directcast(enum.parse(gettype(animalorder), x.animaltypestring), animalorder))) end sub

or if animaltype property on animal points straight @ enum value (which sensible) can skip out conversion to enum...

public function getanimals() list(of animal) homecoming _animallist.orderby(function(x) cint(x.animaltype)) end sub

.net vb.net

No comments:

Post a Comment