Best practice for sharing variables across forms in VB.NET

后端 未结 6 1509
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 05:56

I need to share variables across two forms in VB.NET. One of them is the main form and the other is a child form.

I have been searching, and I have found a few metho

6条回答
  •  执念已碎
    2021-01-07 06:14

    Store your global variables in a Module1.vb file, they must be publicly declared to be accessible from all forms:

    Public X as String
    Public Y as Integer
    

    Then just use them as you would any other variable on any page:

    X = "Hello"
    Y = 10
    
    Textbox1.Text = X
    Textbox2.Text = Y
    

    It's not the safest practice so it shouldn't be used for all your variables. But it is very neat and simple.

提交回复
热议问题