Passing values problem...

EStallworth

Well-known member
Joined
Aug 14, 2006
Messages
75
Location
Destin, FL
Programming Experience
Beginner
I have two forms that I am trying to pass values between. I have referenced each form to the other and am able to use objects from the first form on the second and vice versa. The problem is that I am receiving an error message stating "object reference not set to an instance of an object." Can anyone help me with this?¿ I have used the same technique I am using now on other forms and have had no problems. The only difference is that one of the forms I am using now was a 2k3.Net form that I used the migration wizard to carry over. Could this be the problem?¿


Form1 Reference:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] freq [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form10[/SIZE]
 
[SIZE=2][SIZE=2]#[/SIZE][SIZE=2][COLOR=#0000ff]Region[/COLOR][/SIZE][SIZE=2][COLOR=#800000]" Windows Form Designer generated code "[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] lb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListBox, [/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] mi [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MenuItem, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form10)[/SIZE]
[SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].New()[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ListBox1 = lb[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].menuitem8 = mi[/SIZE]
[SIZE=2]freq = [/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2](f, Form10)[/SIZE]
 
[SIZE=2][COLOR=#008000]'This call is required by the Windows Form Designer.[/COLOR][/SIZE]
[SIZE=2]InitializeComponent()[/SIZE]
[SIZE=2][COLOR=#008000]'Add any initialization after the InitializeComponent() call[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/SIZE]

Form 2 refernce:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] post [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form2[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] cb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ComboBox, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] p [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form2)[/SIZE]
[SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].New()[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].duedatelabel = cb[/SIZE]
[SIZE=2]post = [/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2](p, Form2)[/SIZE]
 
[SIZE=2]InitializeComponent()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Sorry I was in a hurry at about quitting time and forgot to show the lines that are causing errors. Here are the lines that are on form 2. I am trying to cut out a little bit of user error and be able to pass the correct time period to form 2 instead of allowing the user to enter it.

"object reference not set to an instance of an object."

VB.NET:
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2]     TextBox1.Text = [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](post.ComboBox2.Text)[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]     MessageBox.Show(ex.Message)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
 
A great way of dealing with variables is to use Global Variables. I have used this approach in all of my applications, I have read cons against it but to be honest I see more pros than cons personally.

In your project create a new module - I tend to always call it modVariables

Within the module, use Friend to declare your variables, as such;

VB.NET:
Friend varCustomerName As String
Friend varCustomerID As Integer
Friend varMale As Boolean

then on Form 1, you would set something like

VB.NET:
varCustomerName = me.textbox1.text

when you open Form 2, be it from form 1 or a menu or any other form, you simply set

VB.NET:
me.textbox2.text = varCustomerName

As long as you have set the variable to a value, it will appear throughout the life of your application (i.e. the values set to the variables will be cleared once the application has been completely closed).

I hope this can help you in some way.

Regards,
Luke
 
Thanks for the info Luke. That does help with the current scenario I am dealing with. I had tried that approach on a previous problem to no avail but I was labeling them as public and not friend so I think I know why now. I would still like to know what is wrong with the way I tried referencing the two forms. Just FMI.
 
Personally I have never tried doing it the referenced way...When I started my first app and asked on here about it I was instantly shown the global variable path...and never looked back :p

Hopefully someone else will be able to spot the problem for you, unfortunately, I can't!!
 
VB.NET:
Private post As Form2

PublicSubNew(ByRef cb As ComboBox, ByVal p As Form2)
MyBase.New()
Me.duedatelabel = cb
post = DirectCast(p, Form2)

InitializeComponent()
EndSub

One problem here is that you shouldn't really be doing anything until after the call to InitializeComponent. Secondly you don't need to DirectCast p to form2 becuase p is already the type Form2 so it's just a waste to do that.

VB.NET:
Try
     TextBox1.Text = CInt(post.ComboBox2.Text)
Catch ex As Exception
     MessageBox.Show(ex.Message)
EndTry

Here you are trying to convert a string object into a Integer and then assign it to the text property of a textbox. That doen't make sense either.
These may not explan why you are getting the object reference error but they are a few things that I thought i should point out.
 
Back
Top