Link to home
Start Free TrialLog in
Avatar of K Feening
K FeeningFlag for Australia

asked on

VB.Net Variables

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
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

You need to use "Friend" modifier.

will be back with an example shortly.
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
Avatar of K Feening

ASKER

Thanks

But when  I use form2.variable in form1 get error form2 is not declared
I tried dim fm as new form2()
same error
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)
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
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent