Solved

Move Variables Values Between Forms

Posted on 2012-03-31
3
257 Views
Last Modified: 2012-06-27
Hi

I have a form [Form1] that has a [Dim SomeVar as String = "Some_Value"]

Also, this form has a button that upon clicking then it hides [Form1] and shows [Form2]

How do I transfer/use this [SomeVar] value in [Form2] by then?
0
Comment
Question by:alfardan
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37792320
Is there only ever ONE instance of Form1?  Is it always the same instance? (never used the "new" keyword to show it)

If yes, then change it to a SHARED variable:
Public Class Form1
    Public Shared SomeVar As String = "Some_Value"
End Class

Open in new window


Now, from anywhere else, you can access it like this:

    Form1.SomeVar
0
 
LVL 40
ID: 37792378
The best way to do that is to create a constructor in Form 2 that requires a parameter:

Public Sub New (yourVariable As String)
   InitializeComponent()
   'Do what you want with yourVariable
End Sub

It's important to leave the InitializeComponent, it calls the method that creates the form.

Then, in Form1, when you need to pass the value to Form2, do it the following way:

Dim form2 As New Form2(SomeVar)

Your variable will be passed to the form. You can pass many variables if needed, and even create many Sub New, which will give you many ways to initialize the form (this is called overloading), as long as the type of parameters (this is called the signature of the method) are different.

This is a better way than a Shared variable.

First, the shared variable is shared between different uses. That might not cause problems for now, but there are a few situations that could happen during maintenance (an application lives for a while, many years is most cases) where you will end up having strange bugs. There are uses for Shared variables, but this is not one of those.

Second, it becomes visible in all the application. Any form or module in the project will have access to it, even if not initialized by Form1. This can make debugging a lot more difficult because you might have to look everywhere in the application to correct some bugs. Passed as a parameters, the use is limited to where you intend it to be, and you will look more easily where to look for in case of bugs.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37792421
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question