Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a class in vb.net, it performs a bunch of functions on an array. I open a form and display a number of items that are part of the class that opened it. How do I access the class that called it's variables and functions to perform anything? I tried Me.Parent, Me.Owner, ... no luck? I have included the calling subroutine of the calling class below. There are other functions in the calling class I would like to access from the form's buttons. The MachineLogForm is created previously and is simply included as a form in the IDE.

Would appreciate any help.

Thanks, Makulais

''''''''''''calling class''''''''''''''''''''''

VB
Public Class MyClass
Public Sub DisplayCurrentMessages()
       Dim mylogdisplay As New MachineLogForm
       mylogdisplay.ShowDialog()
End Sub
    'do some work here on data displayed in mylogdisplay
Public Sub AnotherFunction

End Sub



''''''''''''''''code from form that mylogdisplay is created from'''''''''''''''''
VB
Imports System.Windows.Forms
Public Class MachineLogForm
    Private Sub pbClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbClearAll.Click
    ''''at this point i want to call AnotherFunction from the instance of the
    '''' class that opened this instance of the form
    '???????????????????????????????????????????????????????
    End Sub
End Class
Posted
Updated 4-Mar-12 6:07am
v2

1 solution

You can't do that. Since the form has no idea what class instance called it, there's no way for you to call back to a method in that type.

This would also tie both forms together so neither could be used again without the other form.

It sounds as though your app design is a bit messed up and you need to reevaluate each forms responsibilities and possibly redesign your data model.

If your form is manipulating data, it really shouldn't be. Encapsulate the data and all the methods to manipulate it in it's own class. Then each form can tell the data class what to do without relying on the other form.

Forms should handle UI functions and command a data model to do what needs to be done, nothing else.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Mar-12 13:39pm    
Very good points, a 5.
OP is recommended to accept this answer formally (green button).
--SA
Manfred Rudolf Bihy 17-Mar-12 15:09pm    
Moved on OP's behalf from non-solution

I understand your points, and it is written that way, the data is all encapsulated and all the methods are also. but i also wanted a display function to be part of the class so that i can reuse it with different instances as a method call from the class. when the method of the class displayed it's data, in that display would be a button to reset the data from the instance of the class that is being displayed.

I would have to say also that my example above may have oversimplified the actual code. And to my knowledge, the opened form does know which instance of the class called it, it can access that information by 'Owner', it just doesn't know or have access to any of 'Owner' subroutines, methods, or variables at design time.

i don't really think that the app design is messed up from the aspect of encapsulation, maybe the fact that i want a self-contained display is so that i don't have to rewrite code outside of the class to display and reset it's data? but that only seems like common-sense to me.

anyway, to accomplish my goals i used the DialogResult and passed back a value that told it to reset the data and then reopened the dialog immediately. There is a slight blink, during the redraw when the dialog/display actually closes and reopens, but it works. I was hoping to get a 'cleaner' way to do it. If someone else has one, by all means, please chime in.
Dave Kreskowiak 17-Mar-12 19:19pm    
but i also wanted a display function to be part of the class

The class does NOTHING but handle the data. Any display garbage violates the rules of encapsulation. If you want the class to display something, you don't display it. You create events that the consumer (your UI code) can subscribe to and get notified of changes or updates. Your data class is allowed to raise events, not make calls into something it has no business calling.

in that display would be a button to reset the data from the instance of the class that is being displayed
Easy enough. Your UI code (form) puts up the button and, when the button is clicked, it can call a method in your data class to reset it.

i don't really think that the app design is messed up from the aspect of encapsulation

Oh, yes it is! If you want to mix business and UI in the same class, it's seriously messed up.

maybe the fact that i want a self-contained display is so that i don't have to rewrite code outside of the class to display and reset it's data

That's just being lazy and the result of a bad degin approach to begin with.
Makulais 17-Mar-12 19:22pm    
After thinking about it a little more, I would have to say that the correct way to implement what I need is to create another class that holds both the forms needed, and the data class, that way i would be able to work through it to handle the information exchange and still have all the code wrapped up in one neat package. I'll call this solved on my own.
Dave Kreskowiak 17-Mar-12 21:22pm    
If you get this to work it'll be the ugliest solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900