Tassos
Tassos

Reputation: 73

Way to pass data between forms (VB 2008)

Is this way a "right" way to pass data between two forms?

'Form1 code

Public Class Form1

Public str As New String = "This is the string"

'...more code

End Class


'Form2 code

Public Class Form2

Public str2 As New string = ""

Private Sub Form2_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

str2 = Form1.str

End Sub

'...more code

End Class

Thank you in advance,

Tassos

Upvotes: 2

Views: 1934

Answers (2)

Denmark
Denmark

Reputation: 538

In form1 code..

put this in button code or something that you will use...

Form2.str2.Tostring = str.Tostring()

I hopes i give you idea....

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

You want to reference a specific instance of Form1, rather than use it's type name. I know VB.Net provides default instances for forms with the same name as the type name, but this is mainly for compatibility and feature parity with old vb6 code, and there are some good reasons not to use the default instances with .Net.

Additionally, most of the time you have a public field, you should use a property instead.

Upvotes: 1

Related Questions