vb.net - Outputting multiple variables defined in a method -
i have series of basic calculations on form triggered form load event:
dim somevariablea integer dim somevariableb integer dim somevariablex integer = 1 dim somevariabley integer = 2 somevariablea = somevariablex + somevariabley somevariableb = somevariablex * somevariabley
i require exact calculations separate form. rather pasting same again, there means can place calculation in method both forms can call upon?
public function somefunction() ' above calculations placed here instead. end function private sub somesub() ' call calculations. somefunction() ' ...now output , use variables function. textbox1.text = somevariablea textbox2.text = somevariableb end sub
ultimately, i'm expecting behaves php's include function.
you running issue of scope. variable declared determines availability. know how make variables visible methods in form:
public class form1 private vara string private var2 integer
these available methods in form because declared @ form level (unlike variable declared (dim
) inside procedure exist locally). make them visible methods in app's forms, declare them in module:
public module1 friend vara string friend var2 integer friend varx datetime
declared in module (1980s style!), become global variables app. there good reason avoid this. easy change value, can have methods accidentally or unwittingly - remember visible procedure might have no reason change them! then, spend time trying locate methods changing value(s) should not be.
a gigantic benefit of oop ability avoid using classes hold data and contain methods manage data - can loading , saving calcualations need. sign might need have variables want global , have methods global, combine them , have class:
public class foo private vara string private var2 integer ' of these things might better properties ' allows subscribers (users of class) change ' values directly: public property somedate datetime public property name string public property value integer public function getsomething(avar integer) integer var2 += avar ' update var2 example return var2 ' return new value end function
to make class available forms:
public module1 friend myfoo foo ' makes visible forms
then create instance of class main form:
public class form1 private sub form_load(.... myfoo = new foo
now, myfoo
instance of foo
class not houses variables, methods manage them:
private sub button_click(.... somevar = myfoo.dosomething(42)
Comments
Post a Comment