GornHorse Posted June 12, 2003 Posted June 12, 2003 Hi, Is there a special way you need to open Form2 from Form1 so that Form2 can access the variables declared in Form1? Ie, i open Form2 from Form1 with: dim frm2 as new Form2() frm2.Show() Then, within Form2, i access the public decalred variable (public var as string) with: tbPassedVal.Text = frm1.var The frm1 is declared within Form2 as: dim frm1 as new Form1() I can see the var from Form2, however, the value is passed as null, when in fact, it should be "BLAH". Why is this happening? Could somebody please advise a solution that correctly passes the variable to Form2? Thanks again, Michelle :confused: Quote
ashrobo Posted June 12, 2003 Posted June 12, 2003 why dont you paste your code here? im confused after reading your post.. Quote
GornHorse Posted June 12, 2003 Author Posted June 12, 2003 Hi, My code snippets are as follows... The two forms are SearchFileRecord and RetrieveFileDetails. (In SearchFileRecord) --------------------- Public valSelectedRow As Integer Public valSelectedFile As String Public valSelectedPart As String Private Sub BOTHERSDataGrid_Files_DoubleClick(ByVal sender As System.Object, ByVal ne As System.EventArgs) Handles BOTHERSDataGrid_Files.DoubleClick valSelectedRow = BOTHERSDataGrid_Files.CurrentCell.RowNumber() valSelectedFile = BOTHERSDataGrid_Files.Item(valSelectedRow, 0) valSelectedPart = BOTHERSDataGrid_Files.Item(valSelectedRow, 1) Dim myfrm As New RetrieveFileDetails() myfrm.Show() End Sub (In RetrieveFileDetails) ------------------------ Dim myformSearchFileRecord As New SearchFileRecord() Dim valFwdSelectedFile As String Dim valFwdSelectedPart As String Dim valFwdSelectedRow As String Private Sub RetrieveFileDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim blah = myformSearchFileRecord.valSelectedFile tbCurrentLocation.Text = blah valFwdSelectedFile = myformSearchFileRecord.valSelectedFile valFwdSelectedPart = myformSearchFileRecord.valSelectedPart valFwdSelectedRow = myformSearchFileRecord.valSelectedRow myformSearchFileRecord.BOTHERS_Connect() BOTHERS_DataDisplay_Docs() myformSearchFileRecord.BOTHERS_Close() BOTHERSDataGrid_Docs.Expand(-1) End Sub Unfortunately, all values retrieved from the SearchFileRecord form (valSelectedFile, valSelectedPart and valSelected Row) are returning "null" when retrived from RetrieveFileDetails; however, they have values when accessed by SearchFileRecord. Your help with this will be much appreciated. Regards, Michelle Quote
ashrobo Posted June 12, 2003 Posted June 12, 2003 from what i see, the null values are correct. here's what i think is happening. 1) you created an instance of SearchFileRecord 2) in that, you created an instance of RetrieveFileDetails 3) you created yet another instance of SearchFileRecord and attempted to get values from that, instead of that created in step 1). Quote
GornHorse Posted June 12, 2003 Author Posted June 12, 2003 Ok then, that makes sense. I went through the debug, and it does create 2 instances of the SearchFileRecord. How can i code it so that it only declares the instance once?? Thanks. Quote
ashrobo Posted June 12, 2003 Posted June 12, 2003 how does the logic of your program go? 1) create an instance (frmSearch) of SearchFileRecord. 2) from it, create an instance of RetrieveFileDetails (frmRetrieve) 3) Get the values from frmSearch in frmRetrieve is that right? Quote
GornHorse Posted June 12, 2003 Author Posted June 12, 2003 I create the instance of RetriveFileDetails -> that goes to RetrieveFileDetails and creates an instance of frmSearch -> when i actually open RetrieveFileDetails, another instance of frmSearch is created. Therefore: 1. instance of RetriveFileDetails 2. instance of frmSearch 3. instance of frmSearch Cheers, and thanks for your help with this. Regards, Michelle Quote
ashrobo Posted June 12, 2003 Posted June 12, 2003 i'm sorry, maybe you can give a senario to aid understanding of the problem. Quote
*Experts* mutant Posted June 12, 2003 *Experts* Posted June 12, 2003 Declare your form as new only once, then keep using that variable so you refer to the same instance all the time. Quote
GornHorse Posted June 12, 2003 Author Posted June 12, 2003 What happens is the following... 1. When i declare 'dim myfrm as new RetrieveFileDetails' within the SearchFileRecord form, the debug shows the next step as going to the RetrieveFileDetails, passing through the 'public sub new()' and therefore initialising 'dim myfrmSearchFileRecord as SearchFileRecord.' 2. When that is initialised, the debug goes back to the SearchFileRecord form and finishes the sub. Then, when i actually '.show' the RetrieveFileDetails form, the debug once again shows it pass through 'public sub new()' and therefore once again initialises 'dim myfrmSearchFileRecord as SearchFileRecord'. Any ideas on where i can put the initialisation of the SearchFileRecord instance so that it is still public to the code within RetrieveFileDetails, yet will only initialise once the form is loaded, and not when the instance of RetrieveFileDetails is created? Your help with this will be much appreciated. Regards, Michelle Quote
GornHorse Posted June 12, 2003 Author Posted June 12, 2003 Hi There! For interested parties, I have figured it out. Needed to make the second form as child of the first form so that the second form. Please see the 'accepted answer' supplied at the link below... http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20450182.html#7697242 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.