Solved

Move Variables Values Between Forms

Posted on 2012-03-31
3
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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