Solved

VB.Net Variables

Posted on 2014-10-30
7
185 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
ID: 40415077
You need to use "Friend" modifier.

will be back with an example shortly.
0
 
LVL 33

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 27

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now