Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

class hierarchy

Status
Not open for further replies.

oinky

Programmer
Oct 29, 2001
45
US
Hi i have two forms, form1 and form2. form1 is my main class. Both form1 and form2 inherits System.Windows.Forms.Form. In order to use the methods and variables in form2, i had to instaniate form2 by doing:

dim frm2 as new form2()

Now in form2 I want to use the variables and methods in form1. I cannot access them. If I instaniate a form1, the form loses the info that it had before. Say in form1 i had a variable "temp" that had a value of 20. If i do

dim frm1 as new form1()
and i do msgbox ( frm1.temp) i will not get 20 because that value isnt there. Is there a way to use variables from my main form?? Please help thanks.
 
You have to pass a copy of your form1 instance variable into your form2 code. You can do this via a public variable (probably bad), or a property (very good). You can then access the methods, etc. of form1 inside form2.

Chip H.
 
I have been trying to pass an instance of my form1 into form2. However, im not having much success. In my form1 I have something like this:

dim frm2 as new Form2(Me)

and in form2, Im guessing this is the constructor that vb .net generated for me so I just changed the parameters

Public Sub New(ByVal frm1 As Object)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

End Sub


then in the code in form2 i try to use

frm1.listbox1.items.add(str)

but it doesnt work saying it cant recognize frm1. Please help thx.
 
Try this:

Make Form1 have a private member variable of type Form2:
Dim m_MyForm2 as Form2

In Form1's New method do this, which will create a new instance of Form2:
m_MyForm2 = New Form2

Then instantiate Form1 from some other code:
' In some other code elsewhere
Dim MyMainForm as Form1
MyMainForm = New Form1

This will call Form1's constructor (the New method), which will create an instance of Form2 (the m_MyForm2 member variable). You can then access the public methods/properties of m_MyForm2 from inside your Form1 (MyMainForm) class.

If you want to access the private objects inside Form2 you will need to either make them public, or provide a public accessor method (a public wrapper around private methods that can protect against inappropiate access). For example, if you want to add items to a listbox of Customers on Form2 (controls are typically private), you'd create a public accessor method called "AddToCustomerListBox" which accepts a customer object, and calls the private listbox.add method. This way you don't expose the entire listbox control by making it public (which would allow callers to delete items too - something that would be bad). The idea is, like I said, to provide limited, controlled access to the private members via public methods or properties.

You're not worried about malicious crackers by doing this, but to protect yourself against your making mistakes later. Falls under the "defensive programming" set of skills. Plus, if you were to rename the listbox at a later point you'd only have to update the accessor function, and not the gazillion places you call it from.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top