Reference to caller form's public variable/function
There' a form(formA) which calls another form(formB).
formA calls formB like this.
Using frm As New formB
frm.ShowDialog(Me)
End Using
In formB, refer to formA's control like this.
CType(Me.Owner, formA).Text1.Text = "AAA"
How do I refer formA's public variable or public function?
Should I use CType(Me.Owner, formA) like control?
CType(Me.Owner, formA).myVar = 1
ret = CType(Me.Owner, formA).myFunc()
or just do like this?
formA.myVar = 1
ret = formA.myFunc()
Re: Reference to caller form's public variable/function
Give it a try. Intellisense will show you the available attributes when you type the ".". Keep in mind that a control is just a public (Friend, actually, in most cases) member variable on the form, and is therefore just like any other public member. You may not have seen the place where each of your controls is declared just like every other member, but it is there. You can find those declarations in the .designer.vb file for the form. From looking at that, you will see that there is nothing special about controls, they are just variables like any other (except that the form designer shows them, but even that isn't all that special). Therefore, since you know how to access one member variable, you know how to access all of them.
Re: Reference to caller form's public variable/function
Actually I tried both ways and works fine and intellisense shows well in both ways.
But I want to know which one is correct.
Re: Reference to caller form's public variable/function
That's a key distinction, and not so easy to answer...unless I just say that I don't like either one.
The problem is the second one: That will only work if you are using the default instance of the form. Default instances were added with VS2005, and are effectively a line like this:
Public formA As New formA
That exact line isn't found anywhere in your project, as the compiler is a bit more efficient than that, but the line is effectively there. So, you have an instance that happens to have the same name as the Type. That's not necessarily a bad thing, and your startup form is almost always going to be a default instance, but you have to be aware of the instance. If you were to write this:
Dim newForm as New formA
formA.SomeFunction
You have now created a new instance of formA, but you called the method on the default instance. The two instances of the form are not the same thing, and the two are not likely to behave the same way. If the function was dependent on member variables, the function would return some odd data.
So, as long as you understand that you are using the default instance, and mean to do so, then I think the second way is somewhat superior, as that cast is going to cost you some infinitessimal amount of time. However, if you are not trying to deal with the default type, then the second way isn't going to work except by accident (if you are dealing with the default type and didn't know it, which is so often the case). As for the first example, that will work, but DirectCast is going to be ever so slightly more effective than CType for performing the cast. There are limitations on DirectCast, but this isn't one of them.
Re: Reference to caller form's public variable/function
Thanks. I need more study about Type Casting.
Once I adopted CType(Me.Owner, formA).
Re: Reference to caller form's public variable/function
CType isn't bad. DirectCast is better where it works, but only because it is faster. You'd have to be performing thousands of them to see the speed difference, though, and even then it would be only a millisecond or two, so you would have a hard time seeing thousands of them. However, DirectCast does have some limitations that CType doesn't have. Therefore, don't worry too much about using CType.