如何从函数调用私有子 [英] How To Call A Private Sub From A Function
问题描述
我正在vb.net
我已将Web应用程序连接到数据库.
我现在想使用导航器按钮浏览表格的各行.
应该是当我双击default.aspx中的一个按钮时..应该在default.aspx.vb中调出一个私人子目录
但是它不起作用!当我双击按钮时,它会导致一个功能button1_onclick
can,我可以从此功能调用私人子吗??
我试过打电话给mysub()
其中mysub是私有子项的名称,但不起作用
I am working vb.net
I have connected the web app to database.
i want to use now navigator buttons to go through the rows of the table.
it is supposed that when i double click a button in default.aspx.. should bring up a private sub in default.aspx.vb
BUT it doesnt work!! When I double click the button it leads to a function button1_onclick
hoow can i call the private sub from this function???
I have tried call mysub()
where mysub is the name of the private sub but it doesnt work
推荐答案
如果您的私有函数与您的单击不在同一类中事件处理程序,您不能这样做...这是因为私有函数是其类的私有.因此,假设您具有以下条件:
If your private function is not in the same class as your click event handler, you can''t do it... that is because private functions are private to their class. So let''s say you have the following:
Module MainFunctions
Private Sub MySub()
' do something here
End Sub
End Module
并且从default.aspx内部尝试调用MySub(),因为MySub被声明为私有的,并且您正在default.aspx类内部进行调用,因此它将无法工作. (该页面后面有一个类...该类是您在其中找到button1_Click事件处理程序的代码.)
如果您希望能够从事件处理程序中调用MySub,则MySub要么需要是Public或Friend. (我建议Friend)这对于模块(在VB中是静态类)或常规类中的函数和子类都是正确的.当然,对于一个类,您必须先创建该类的实例,然后才能调用该方法:
And from within default.aspx you try call MySub() it will not work because MySub is declared as private and you are making the call from within the default.aspx class. (There is a class behind that page... that is the code-behind where you are finding the button1_Click event handler.)
If you want to be able to call MySub from within the event handler, MySub either needs to be Public or Friend. (I suggest Friend) This is true for functions and subs in either a module (which is a static class in VB) or a regular class. Of course, for a class you have to create an instance of the class before you can call the method:
Dim myClass as new MyClass
myClass.MySub()
如果您要从aspx调用方法以在其后进行编码,则该方法称为应该具有protected
访问修饰符,而不是private
If you are calling a method from your aspx to code behind, the method called should have theprotected
access modifier instead ofprivate
这篇关于如何从函数调用私有子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!