VB.net如何从类打开/调用的形式访问类变量. [英] VB.net how to access class variables from a form the class opens/calls.

查看:74
本文介绍了VB.net如何从类打开/调用的形式访问类变量.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb.net中创建了一个类,它在数组上执行了很多功能.我打开一个表单,并显示一些属于打开该表单的类的项.如何访问调用其变量和函数的类以执行任何操作?我尝试过Me.Parent,Me.Owner ......没有运气吗?我已经在下面包含了调用类的调用子例程.我想从表单的按钮访问调用类中的其他函数. MachineLogForm是先前创建的,仅作为表单包含在IDE中.

不胜感激.

谢谢,Makulais

''''''''''''''''''''''''呼叫类别'''''''''''''''''''''''''' ''''''''''''''''''

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''''''''''''''''''''''''''''''''''''''''''''

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




''''''''''''''''''''''''''''''''''''的代码来自从''''''''''创建mylogdisplay的形式''''''''''''''''''''''




''''''''''''''''''''''''''''''''code from form that mylogdisplay is created from''''''''''''''''''''''''''''''''''

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

推荐答案

你不能那样做.由于表单不知道哪个类实例调用了它,因此您无法回调该类型的方法.

这还将把两种形式结合在一起,因此没有其他形式就不能再次使用.

听起来好像您的应用程序设计有些混乱,您需要重新评估每种表单的职责,并可能重新设计数据模型.

如果您的表单正在处理数据,则实际上不应该这样做.将数据及其所有方法封装在自己的类中.然后,每种形式都可以告诉数据类在不依赖另一种形式的情况下该怎么做.

表单应处理UI函数并命令数据模型执行所需要做的事情,而无需执行其他操作.
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.


这篇关于VB.net如何从类打开/调用的形式访问类变量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆