Solved

VB.Net Variables

Posted on 2014-10-30
7
181 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 33

Expert Comment

by:Mike Eghtebas
Comment Utility
You need to use "Friend" modifier.

will be back with an example shortly.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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 27

Accepted Solution

by:
Ark earned 500 total points
Comment Utility
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
Comment Utility
Excellent
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

8 Experts available now in Live!

Get 1:1 Help Now