vb.net - How to limit the scope of a variable to the function when declared in an If statement -
i need limit scope of variable function resides need declare within if statement it's type change depending. i'm working within vb.net
public function coursedatatable() if radiocourses.checked dim searchby new classsearchcourses elseif radioattendees.checked dim searchby new classsearchattendees end if
the obvious problem variable doesn't persist outside of if statement. want limit scope of variable because a, it's used else , b, memory leakage, class end holding whole sql tables , don't want persisting when it's not needed. can't use inheritance or polymorph here because i'm working legacy system.
this rework (i'm struggling think of different way of approaching evidently) can't find in msdn allows procedure scope ignores other blocks @ declaration.
it still possible use polymorphism in legacy system. can find common functionality must exist between 2 in order want reuse same variable. can create wrapper classes each of these legacy classes. wrapper class implement common interface , call underlying legacy implementation. declare variable common interface , create appropriate wrapper class instance inside of if statements.
edit: if have ability modify legacy classes @ all, simpler solution create common interface both of legacy classes can implement. give polymorphic functionality desire without need of wrapper classes. vb.net provides ability implement interface in way interface methods exposed interface reference. this, mark interface implementation methods private.
Comments
Post a Comment