vb.net - Loop through a class with enum array -
i have class 1 of fields enum type array:
public enum invalidemailtype unkown trailingspace innerspace trailingperiod missingatsign multipleatsigns fakeemail phoneextension end enum public class customerclass public customername string public ldccode integer public ldcname string public ldcaccountno string public commodity string public email string public validemail boolean public suggestion string public errortype = [enum].getvalues(gettype(invalidemailtype)) public errordescription string end class
as add values errortype array, need write single errordescription user read easily. errordescription field works summary of potential errors in 1 word.
but first need determine how many instances of invalidemailtype.unkown there.
for wrote function count number of unknowns.
my problem how loop through array inside class , match enum?
this tried far:
public shared function errotypemessage(customer customerclass) string dim instances integer = 0 each value in customer.errortype if value. <> invalidemailtype.unkown 'here problem end if next end function
update. here new function. still cannot accurate match
public shared function errotypemessage(customer customerclass) string dim instances integer = 0 errotypemessage = nothing each value in customer.errortype if value <> invalidemailtype.unkown 'does not match instances = +1 else errotypemessage = value.tostring end if next if instances > 1 errotypemessage = "multiple" elseif instances = 1 'do nothing else errotypemessage = "unkown" end if end function
i have solved problem.
public shared function errotypemessage(customer customerclass) string dim instances integer = 0 errotypemessage = nothing dim temp string each value in customer.errortype temp = directcast([enum].parse(gettype(invalidemailtype), value.tostring), invalidemailtype) if temp <> invalidemailtype.unkown errotypemessage = temp instances += 1 end if next if instances > 1 errotypemessage = "multiple" elseif instances = 1 errotypemessage = ctype(errotypemessage, invalidemailtype).tostring() elseif instances = 0 errotypemessage = "unkown" end if end function
your email error variable can array, enum flag type , combine errors:
<flags> enum invalidemailtype noerror = 0 unkown = 1 ' not needed? trailingspace = 2 innerspace = 4 trailingperiod = 8 missingatsign = 16 multipleatsigns = 32 fakeemail = 64 phoneextension = 128 end enum
in class:
public errortype invalidemailtype = invalidemailtype.none
in code validate email addy, or enum values accumulate errors:
errortype = errortype or invalidemailtype.trailingperiod ' example
to test condition, use and:
if errtype , invalidemailtype.fakeemail = invalidemailtype.fakeemail ' take action here end if
i not sure how detect unknown.
Comments
Post a Comment