?
Solved

VB.Net Variables

Posted on 2014-10-30
7
Medium Priority
?
194 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

719 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