2017-02-28 55 views
-2

要求的全局变量这是一个赋值,它是其中一个要求。我不太了解全局变量。我试图设置shoestotal作为一个全局变量,然后以另一种形式使用它 这是我的第一种形式的代码,我不太确定从哪里开始下一个。我试过了,但它给了我$ 0如何使用全局变量

私人小组Frm7_Load(发送者为对象,E作为EventArgs的)把手MyBase.Load TxtBox7.Text = FormatCurrency(Frm3.Shoestotal) 结束小组

第一种形式

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 
+0

这心不是一个全局变量在所有 - 它是一种形式的水平VAR,你有隐式转换一个更大的问题 - 打开选项严格。因为,表单变量应该工作 - 我猜你会遇到与默认表单实例有关的问题。显示如何尝试从另一个表格使用 – Plutonix

+0

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

+0

'Frm3'是一个类名。你需要一个适当的表单实例来确保你引用了你的想法。但那不是全球性的,只是形成水平, – Plutonix

回答

0

添加一个模块,并有将其定义为公共...

2

不要。

哦,还不够?

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

+0

我需要它作为一项任务,它的要求 – Rob