vb.net - How do I determine if a class member exists? -
i have public subroutine called many classes. however, need in subroutine pertains small number of classes call it. instead of going , adding property of existing classes, check see see if class has property , if so, it. can't seem figure out how check existence of member without getting error.
for example:
public class_1 public a1 string = "" public sub new() ' when button clicked call subroutine "check()" end sub end class public class_2 public a1 string = "" public a2 integer = 0 public sub new() ' when button clicked call subroutine "check()" end sub end class public class whatever public sub check(sender object) if sender.a2 = 0 ' end if end sub end class
i have tried such things as
if not(sender.a2 nothing) if isnothing(sender.a2)
but can't past fact error using "sender.a2" since a2 not member of sender.
how can check see if a2 member of sending class without using "sender.a2"?
if want see field exists need this:
dim fi fieldinfo = sender.gettype().getfield("nameoffieldhere") if fi isnot nothing 'field exists value dim o object = fi.getvalue(sender) end if
take @ documentation type.getfield method (string) there overloads available too.
Comments
Post a Comment