Solved

Move Variables Values Between Forms

Posted on 2012-03-31
3
254 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 74

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now