Solved

VB.Net Variables

Posted on 2014-10-30
7
190 Views
Last Modified: 2014-10-31
Hi Experts

Form1 uses the following  

Public Overridable Function Save(Optional ByVal trainsaction as IDatatransaction = Nothing) asString
   Do something
   Do Something else
   variable = True
End function

Form2 Uses the following

Private Sub Form2_FormClosing(ByVal sender as System.Object, ByVal e As System.windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    if variable = True then  
       Do stuff
    end if
    if variable = False then  
       More Stuff
    end if
End sub

Is there a way to pass a variable = True if form1 run/Processed  - Public Overridable Function Save
to form 2 so when it closes if can run do stuff if the variable = True or run  More Stuff if variable = False
also where would you declare the variable

Thanks
0
Comment
Question by:Kevinfeening
7 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40415077
You need to use "Friend" modifier.

will be back with an example shortly.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40415083
Form1 uses the following  

Friend variable As Boolean

Public Overridable Function Save(Optional ByVal trainsaction as IDatatransaction = Nothing) asString
   Do something
   Do Something else
   Form2.variable = True
End function

Form2 Uses the following

Friend variable As Boolean

Private Sub Form2_FormClosing(ByVal sender as System.Object, ByVal e As System.windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    if variable = True then  
       Do stuff
    end if
    if variable = False then  
       More Stuff
    end if
End sub
0
 

Author Comment

by:Kevinfeening
ID: 40415207
Thanks

But when  I use form2.variable in form1 get error form2 is not declared
I tried dim fm as new form2()
same error
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 40
ID: 40415228
It depends on the relation between the 2 forms.

If Form1 is created and showed from somewhere inside Form2, then you can use the variable suggested by eghtebas, but by using a syntax like the following. In order to do so, you should not close Form2 from inside itself. Hide it instead of closing it.

Dim frm as New Form1
...
If frm.variable then
   Do stuff
Else
   Do something else 'You cannot do More Stuff, because the variable can never be True and False at the same time
End if
frm.Close  'This is where Form2 will be closed, after you have retrieved its value.

If it is the reverse, and Form2 is created from inside Form1, then create a constructor that will receive the variable:

In Form2, add the following constructor:

Public Sub New(parameter As Boolean)
   InitializeComponents 'The editor will automatically write that line, do not remove it
  'Here, record the parameter in a variable that you will be able to use in your form
End Sub

When you create Form2 from Form1, do it the following way:

Dim frm As New Form2(variable)
0
 

Author Comment

by:Kevinfeening
ID: 40415333
the project has been created by a developer above my expertise when a form is displayed all the relevant fields are on the form but the save and cancel buttons are added in the program
When the save button is clicked if calls a BusinesObjectform and executes Public Overridable Function Save(
when you try to add any Dim frm as New Form1, form2 any name you get error
New cannot be used on a class that has been declared mustinherit  

I think I have a problem apart from creating a work file and adding data if the save is clicked and checking the file when closing the second form
0
 
LVL 28

Accepted Solution

by:
Ark earned 500 total points
ID: 40415367
Public Class MySharedVars
    Public Shared Variable_1 As Boolean
    Public Shared Variable_2 As String
    Public Shared Variable_3 As Integer
End Class
'Anywhere in your project:
MySharedVars.Variable_1 = True
'Anywhere in your project:
If MySharedVars.Variable_1 = True Then

Open in new window

0
 

Author Closing Comment

by:Kevinfeening
ID: 40415401
Excellent
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 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