Results 1 to 6 of 6

Thread: Reference to caller form's public variable/function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    433

    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()

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    433

    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.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    433

    Re: Reference to caller form's public variable/function

    Thanks. I need more study about Type Casting.
    Once I adopted CType(Me.Owner, formA).

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width