Rob
Rob

Reputation: 13

How to use global variables?

GLOBAL VARIABLES REQUIRED This is for an assignment and it is one of the requirements. I don't understand much about global variables. I am trying to set shoestotal as a global variable and then use it in another form.

This my code for first form and I am not quite sure where to start for the next. I tried, but it gives me $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

Upvotes: 0

Views: 120

Answers (3)

Systaniec
Systaniec

Reputation: 1

Instead of Dim, use Public Shared.

Public Shared globalVar as String

And whenever you want to call it on another form just simply specify the name of the form.variable name.

form1.globalVar

Upvotes: 0

Trevor_G
Trevor_G

Reputation: 1331

Add a module and define it there as Public...

Upvotes: 2

Jim Deville
Jim Deville

Reputation: 10662

Don't.

Oh, not enough?

Globals are a bad practice in any language. They end up polluting one namespace making it hard to manage, follow and reason about. They can be changed anywhere, leading to difficulties debugging. Good software should be modular, composable and encapsulated promoting the ideas of reuse and allowing people to reason about small pieces of code in isolation.

Upvotes: 2

Related Questions