如何使用全局变量

时间:2017-02-28 20:51:33

标签: vb.net

需要全局变量这是一项任务,这是其中一项要求。我对全局变量了解不多。我试图将shoestotal设置为全局变量,然后以另一种形式使用它  这是我的第一个表单的代码,我不太确定下一步的开始。我试过了,但它给了我$ 0

Private Sub Frm7_Load(sender as Object,e As EventArgs)处理MyBase.Load         TxtBox7.Text = FormatCurrency(Frm3.Shoestotal)     结束子

FIRST FORM

Public Class Frm3
Public Shoestotal As Single

Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Lbl3.Click

End Sub

Private Sub Btn2_Click(sender As Object, e As EventArgs) Handles Btn2.Click
    Me.Close()
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Btn4.Click
    TxtBox1.Text = ""
    TxtBox2.Text = ""
    TxtBox3.Text = ""
    TxtBox4.Text = ""
    TxtBox5.Text = ""
    TxtBox6.Text = ""
    TxtBox7.Text = ""
    TxtBox8.Text = ""
    TxtBox9.Text = ""
    TxtBox10.Text = ""
    TxtBox11.Text = ""
End Sub

Private Sub Btn3_Click(sender As Object, e As EventArgs) Handles Btn3.Click
    Frm2.Show()
    Me.Close()
End Sub
Private cntstyle = 0
Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles Btn1.Click

    Dim blackprice As Single = 43.0
    Dim classyheelprice As Single = 48.95
    Dim redprice As Single = 35.95
    Dim weddingprice As Single = 155.65
    Dim sportsprice As Single = 50
    Dim price1 As Single
    Dim price2 As Single
    Dim price3 As Single
    Dim price4 As Single
    Dim price5 As Single

    If TxtBox1.Text.Length <> 0 Then
        Dim qty1 As Integer = Int32.Parse(TxtBox1.Text.ToString())
        price1 = qty1 * blackprice
    End If
    If TxtBox3.Text.Length <> 0 Then
        Dim qty2 As Integer = Int32.Parse(TxtBox3.Text.ToString())
        price2 = qty2 * classyheelprice
    End If
    If TxtBox4.Text.Length <> 0 Then
        Dim qty3 As Integer = Int32.Parse(TxtBox4.Text.ToString())
        price3 = qty3 * redprice
    End If
    If TxtBox5.Text.Length <> 0 Then
        Dim qty4 As Integer = Int32.Parse(TxtBox5.Text.ToString())
        price4 = qty4 * weddingprice
    End If
    If TxtBox6.Text.Length <> 0 Then
        Dim qty5 As Integer = Int32.Parse(TxtBox6.Text.ToString())
        price5 = qty5 * sportsprice
    End If
    Shoestotal = price1 + price2 + price3 + price4 + price5
    TxtBox11.Text = FormatCurrency(Shoestotal)
    TxtBox2.Text = FormatCurrency(price1)
    TxtBox7.Text = FormatCurrency(price2)
    TxtBox10.Text = FormatCurrency(price3)
    TxtBox9.Text = FormatCurrency(price4)
    TxtBox8.Text = FormatCurrency(price5)
    If TxtBox1.Text.Length <> 0 Then
        cntstyle += 1
    End If
    If TxtBox3.Text.Length <> 0 Then
        cntstyle += 1
    End If
    If TxtBox4.Text.Length <> 0 Then
        cntstyle += 1
    End If
    If TxtBox5.Text.Length <> 0 Then
        cntstyle += 1
    End If
    If TxtBox6.Text.Length <> 0 Then
        cntstyle += 1
    End If
    If cntstyle > 4 Then
        Btn4.PerformClick()
        MessageBox.Show("Only 3 styles can be chosen")

    End If
End Sub

3 个答案:

答案 0 :(得分:2)

唐&#39;吨

哦,还不够?

全球语言在任何语言中都是不好的做法。他们最终污染了一个名称空间,使其难以管理,遵循和推理。它们可以在任何地方更改,导致调试困难。好的软件应该是模块化的,可组合的和封装的,以促进重用的想法,并允许人们孤立地推理小块代码。

答案 1 :(得分:1)

添加模块并将其定义为Public ...

答案 2 :(得分:0)

使用“公共共享”代替Dim。

Public Shared globalVar as String

只要您想在其他表单上调用它,只需指定表单的名称即可。变量名称。

form1.globalVar
相关问题