开发者

Visual Basic setting a global variable making my code DRY

How do I optimize my code making it DRY approach. I want to make my variable to be on public/general so i can re-use it

Public Class BasicForm

Dim Product1, Product2, Product3, As Integer
Dim firstName, lastName As String

Private Sub btn_getValue_Click(ByVal开发者_StackOverflow sender As System.Object, ByVal e As System.EventArgs) Handles btn_getValue.Click


    'Set variables'
    Product1 = Val(tx_productfield1.Text)
    Product2 = Val(tx_productfield2.Text)
    Product3 = Val(tx_productfield3.Text)


    'Calculate'
    tx_totalValue.Text = Product1 + Product2 + Product3


End Sub End Class

I want to move the variables (product1,product2) to somewhere else that I can set it one time and easily access it with other control. What I did before is I alway set the variables to every control.

Please advice.

Thanks!


I'm not sure I'm getting your question, but you know your variables are instance variables, because you're working in a class, right?

Your "product" variables should be private instances, or public properties. If you use the private instances, give a way to access those variables, say Getter methods. Otherwise the "Property" is just fine.

Public Class BasicForm

    Public Function getProduct1() As Integer
        return Product1
    End Function

    '... other getters here


    Private Product1 As Integer
    Private Product2 As Integer
    Private Product3 As Integer
End Class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜