-1

I have 20+ MDI forms with consistently named Public variables. When the child form closes a method on the MDI Parent is called passing Me as a generic form type. How can I access the public variables by name via the Form reference? I only need to read the variables. Of course the Variables() method does not exist...

Public Sub CleanupForm(ByVal frm As Form)
    Dim sTable_Name As String = frm.Variables("TABLE_NAME") ' Public at form level
    Dim cLock As clsRecLocks
    cLock = frm.Variables("Rec_Lock")
    cLock.DeleteThisLock()
    '..

I've seen some posts on similar requests but most start out with "don't do it that way..." then go off in the weeds not answering the question. I concede it is poor design. I can't change all the calling forms in the short term so I need to use this approach.

VS2010, VB.Net, Win Forms, .Net 2.0


I was able to get to a simple variable using CallByName:

    Try
        Dim s As String = CallByName(frm, "TABLE_NAME", CallType.Get)
        Stop
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

On to the class object. Perhaps I can add a default Get for the class that returns the ID I need.


Default property won't work as the Locks object was not declared Public - at least for the CallByName() approach.

Can Reflection get to form level variables not declared Public? Seems like a security issue, but...

Can I get a "Parent" reference in the instantiated Locks class? i.e. A reference to the form that established the Locks object? I can change the clsRecLocks() class.


I found a property I could get to that told me the form was "read-only" and I can use that tidbit to delete the correct (or more correct - still not 100%) lock record. So the bug is 90% fixed. I think I need update all the forms with code that records the info I need to get to 100%.

Thanks to all!

8
  • 1
    Don't do it that way. But if you don't want to use the Interface keyword to do it the right way then you'll need to use Reflection. Like frm.GetType().GetField("Rec_Lock"). Well covered in any book about .NET programming, have a look. Sep 30, 2013 at 18:13
  • Is the MDI Parent trying to 'cleanup' vars on the children? I am confused about the actors in this. Sep 30, 2013 at 18:18
  • "I concede it is poor design." If you're content to keep bad-design, and the variables are PUBLIC as you state, then you use the legacy CallByName() function to retrieve the values. It's slightly easier than using Reflection...
    – Idle_Mind
    Sep 30, 2013 at 18:24
  • Thanks @HighCore - in the original design we failed to record enough detail to completely clean up after ourselves. The value I need is the Id for a system wide lock that locks across several tables. That value is in the cLock object.
    – rheitzman
    Sep 30, 2013 at 18:25
  • A better design would have all of the MdiChildren implement an INTERFACE that exposes all the common variables.
    – Idle_Mind
    Sep 30, 2013 at 18:25

1 Answer 1

0

Poor design but you can do this:

Public Sub CleanupForm(ByVal frm As Form)
  Dim frmTest as object = frm
  ? = frmTest.TABLE_NAME
  ? = frmTest.Rec_Lock
End Sub

This will compile and if the variables exist, it will return them but if not, you get an error.

Converting to an interface after the fact is not that hard, you should do it now rather than later.

2
  • OK, but frmTest is one of 50 different forms. If I could Dim frm as ("frmTest") it would work, but I don't know how to do that. Seems to me a saw a similar post (declaring a Form by name) years ago but I don't even know what to search for.
    – rheitzman
    Sep 30, 2013 at 19:33
  • See edit, this was code to put into your CleanupForm method.
    – Steve
    Sep 30, 2013 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.