NEWBIE - Accessing Variables stored in another form

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I understand that I need to use a "Friend" declaration for these variables,
but I've searched all over the internet and can't figure out how to retrieve
and set the value of variables that are stored in forms outside of the one I
am coding in.

Can somebody provide me with some direction or an example? Thank you
kindly.

-Dave
 
We don't have enough information to answer your question yet.

? In the form you are coding in, do you have a reference to the form you
want to get/set values for?
? Is the form you want to get/set to calling the second form or vice versa?
? How are the two forms related?

Remember, there is nothing special about a "form" A form is really just
another class. It has the same rule and regulation applied to it as and
other class. You wouldn't try to access the members of a class that you
don't have access to, so you can't do it to a "form class" either.

Chris
 
To answer your questions:

1. No - How do I create this reference?
2. I am opening a Form2 from Form1. I want to retrieve values from
variables stored in Form1 and put them on Form2.
3. I do not think the two forms are related. How do I set up a
relationship between the two?

Thanks,
-Dave
 
Ok, that's enough information. Two ways to do what you want.

1. Pass a reference to Form2 of Form1. Make properties on Form1 to give
access to vars that you want. You have to use this one (generally) if you
want to make changes to Form1's Data.

2. Pass in the data you want to Form2 and just use it in the form. You
won't make changes to Form1 this way.

Here's how you do it either way. (This was typed in here, hasn't been
tested)
Class Form1
inherits Form
Public Property ShowVar()
'Implement This
End Property

Private Sub OpenF2()
'Here we are passing in the reference to Form1
'You could also just pass in data instead of a reference.
Dim F2 as New Form2(Me)
End Sub

End Class

Class Form2
inherits Form

Dim RefToForm1 as Form

'If you just wanted data to come in, change the
'Data type in this function.
sub new(byRef Value as Form1)
RefToForm1 = Value
'I can now access form1's properties
end Sub

end class

Hope this helps. Post any questions.
Chris
 
Chris,
Thanks for the great help. At the bottom of your example you mentioned:
'I can now access form1's properties
How can I now, say, access the 3rd item in arraylist1 that was declared in
Form1's code?

Thanks,
-Dave
 
Two ways to do it. 1. Return the ArrayList object from the property. 2.
Make it and index property

Public Class Example
Dim A As ArrayList
Public ReadOnly Property GetItem(ByVal I As Integer) As Object
Get
Return A.Item(I)
End Get
End Property
End Class

Chris
 
Chris,

Thanks. What would the code look like for option 1 (Returning the ArrayList
object from the property).
 
Chris,

I am able now to access the shared variables from Form1 (in the vs.net
development environment), but when I run the code, it throws an unhandled
exception, "system.NullReferenceException'

Here is the code it breaks on:

Private Sub frm_GoTo_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

lbl_CurrentWordNumber.Text = Form1.Counter

End Sub
 
Dave,

Try to avoid this methods.

It is not good OOP to use the forms (UI) as your datacontainers, create
seperate classes for that.

I hope this helps?

Cor
 
Chris,

I figured it out. I had to declare form2 on the top of my form1 class.
Phew! I couldn't have gotten there without your help. Thanks.
 
When in debug what is the value of Form1.Counter? What is the value type of
Form1.Counter? Can you show the code that defines the property of Counter?

My guess is you just didn't initialize whatever Counter is returning.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top