vb.net - Visual Basic keeps reseting my variables to 0 each time I leave and re-enter a class -
i have form obtain financial information user including loan amount, rate, , term. i'm supposed call class work , return answer form. however, reason, whenever program moves out of class, form, class, variables have class reset 0. code below. awesome.
this code form:
'preform calculation when button clicked private sub buttoncalc_click(sender object, e eventargs) handles buttoncalc.click dim loanamount new finclass dim rate new finclass dim term new finclass dim payment new finclass loanamount.getloanamount = textboxmortgageamount.text rate.getannualrate = comboboxannualrate.text.replace("%", "") term.getterminyears = comboboxterm.selecteditem textboxmonthlypayment.text = payment.setmonthlypayment end sub
and code associated class:
public class finclass private loanamt decimal private termvalue decimal private ratevalue decimal public paymentvalue decimal public writeonly property getloanamount set(value) if isnumeric(value) andalso value > 1000 andalso value < 10000000 me.loanamt = value else messagebox.show("mortgage amount must number between $1,000 , $10,000,000", "invalid number", messageboxbuttons.ok, messageboxicon.error) end if end set end property public writeonly property getannualrate set(value) ratevalue = value.replace("%", "") if ratevalue < 1 ratevalue *= 100 end if end set end property public writeonly property getterminyears set(value) termvalue = value end set end property public readonly property setmonthlypayment return -pmt((ratevalue / 1200), termvalue * 12, me.loanamt) end end property end class
i don't work in vs looks creating 4 instances of class. i'm pretty sure not correct.
private sub buttoncalc_click(sender object, e eventargs) handles buttoncalc.click dim loanamount new finclass '## creates new finclass object dim rate new finclass '## creates new finclass object dim term new finclass '## creates new finclass object dim payment new finclass '## creates new finclass object loanamount.getloanamount = textboxmortgageamount.text rate.getannualrate = comboboxannualrate.text.replace("%", "") term.getterminyears = comboboxterm.selecteditem textboxmonthlypayment.text = payment.setmonthlypayment end sub
so each of objects different instance of finclass
object. each time "re-enter" class, you're entering different instace of class object, why variables value = 0. have not been instantiated or set, yet.
try instead, create 1 class object manipulate methods in class module:
private sub buttoncalc_click(sender object, e eventargs) handles buttoncalc.click dim objfinclass new finclass objfinclass.getloanamount = textboxmortgageamount.text objfinclass.getannualrate = comboboxannualrate.text.replace("%", "") objfinclass.getterminyears = comboboxterm.selecteditem textboxmonthlypayment.text = objfinclass.setmonthlypayment end sub
Comments
Post a Comment