Solved

Move Variables Values Between Forms

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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 video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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