首页 文章

如何使用全局变量

提问于
浏览
-2

GLOBAL VARIABLES REQUIRED 这是一项任务,这是其中一项要求 . 我对全局变量了解不多 . 我试图将shoestotal设置为全局变量,然后以另一种形式使用它,这是我的第一种形式的代码,我不太确定下一步的起点 . 我试过了,但它给了我0美元

Private Sub Frm7_Load(sender as Object,e As EventArgs)Handles MyBase.Load TxtBox7.Text = FormatCurrency(Frm3.Shoestotal)End Sub

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 回答

  • 2

    而不是Dim,使用Public Shared .

    Public Shared globalVar as String
    

    每当你想在另一个表单上调用它时,只需指定form.variable名称的名称即可 .

    form1.globalVar
    
  • 1

    别 .

    哦,还不够?

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

  • 0

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

相关问题