I'm declaring two strings into my main form this way:
Public Shared SerNum As String = vbNullString
Public Shared SKey As String = vbNullString
Then I give some values to them. After that, I open another form and I try to get values from the two variables but only SerNum
preserves his value while SKey
turns out to be Nothing
.
I repeatedly checked my code but I didn't found a reason for this to happen.
The second form is showed immediatly after giving values.
What can I check to find the error?
At the moment I solved by using a Public Shared Dictionary(of String, String)
and putting both strings into it, but I would like to understand where I'm wrong.
EDIT
I found the mistake: SKey
was also declared into my sub
so the value wasn't assigned to the Public Shared variable
but to the local variable
.
I thought I had 'commented' that row...
I have a proposition for acceding to your variable from other form : In From main let's name it "From1" must be like :
Public Class Form1
Public SerNum As String = vbNullString
Public SKey As String = vbNullString
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fr As New Form2(Me)
SerNum = "ValueNum"
SKey = "ValueKey"
fr.Show()
End Sub
End Class
In your seconde from "Form2" :
Public Class Form2
Dim fr As New Form1
Public Sub New(fr As Form1)
InitializeComponent()
Me.fr = fr
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(fr.SKey)
MsgBox(fr.SerNum)
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.