kevinmeredith
asked on
Object reference Error keeps coming up
I'm writing some code that passes strings between two forms. At first i was passing them like this:
FORM1:
public user_name as string
public proj_codes() as string
FORM2:
dim frmMain as form1
frmMain.user_name = "test"
frmMain.proj_codes = array
My code would be okay for the "frmMain.user_name = "test"" line but would put up the error
An unhandled exception of type 'System.NullReferenceExcep tion' occurred in system.windows.forms.dll
Additional information: Object reference not set to an instance of an object.
For the second line. So i tried to put it in a module and it works. but now i have a subroutine being called from form2 that is in the module to run a subroutine in form1. The same error keeps popping up, but this time at the beginning of Form1: Public Class Form1. Am i doing anything wrong. All i want to do is pass some strings from Form2 into Form1 after a button is clicked. thanks in advance for any help!
FORM1:
public user_name as string
public proj_codes() as string
FORM2:
dim frmMain as form1
frmMain.user_name = "test"
frmMain.proj_codes = array
My code would be okay for the "frmMain.user_name = "test"" line but would put up the error
An unhandled exception of type 'System.NullReferenceExcep
Additional information: Object reference not set to an instance of an object.
For the second line. So i tried to put it in a module and it works. but now i have a subroutine being called from form2 that is in the module to run a subroutine in form1. The same error keeps popping up, but this time at the beginning of Form1: Public Class Form1. Am i doing anything wrong. All i want to do is pass some strings from Form2 into Form1 after a button is clicked. thanks in advance for any help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The command:
frmMain as form1 = new Form1
makes the a new window pop up, so that doesn't work.
Haqipmc, your answer works for strings. But how can i call a subroutine in form1 from form2? once form2 is completed, i need to update form1. can this be done the same way? thanks for your help!
frmMain as form1 = new Form1
makes the a new window pop up, so that doesn't work.
Haqipmc, your answer works for strings. But how can i call a subroutine in form1 from form2? once form2 is completed, i need to update form1. can this be done the same way? thanks for your help!
Yes, kevinmeredith.. shared subs and functions work just like shared fiels (are the same to all the instances of a class)... but i think this is not what you need here.
If you want to update form1 from form2(or from elsewhere) you should never use close method on form1. Calling this method all resources created within the object are closed and the form is disposed. Use instead Hide and Show methods on form1. This way you have an instance of form1 that can use to update all its public fields or properties.
ASKER
haqipmc,
I don't know where you came up with the idea that i use the close method. I never use. What i'm doing is opening up a Preferences Form to save some constants. Whem i'm done with the Prefernces form, i send the data to form1 and close form2. But i want to start a function that will update all the variables in form one, including some control objects. When i try to do this, i get the Object Refence Error. This is my main question. Thanks for your help btw!
I don't know where you came up with the idea that i use the close method. I never use. What i'm doing is opening up a Preferences Form to save some constants. Whem i'm done with the Prefernces form, i send the data to form1 and close form2. But i want to start a function that will update all the variables in form one, including some control objects. When i try to do this, i get the Object Refence Error. This is my main question. Thanks for your help btw!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked Idle_mind. thanks a bunch!
Bob