在函数中传递表单对象 [英] Passing a form object in a function

查看:45
本文介绍了在函数中传递表单对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我想将表单(我创建的表单)传递给

函数,我将声明设置为


例如


Dim p as existingForm

OpenForm(p,existingForm)

函数(P as windows.forms.form,thisForm为*******)

p = NEW thisForm

.....


我把什么放在*******?

解决方案

作为ExistingForm。如果我正在读这个权利,你甚至不需要通过

P.

" Sebastian Santacroce" < SE ****** @ ilogic.com>在消息中写道

news:82 **************************** @ phx.gbl ... < blockquote class =post_quotes>
如果我想将表单(我创建的表单)传递给
函数,我将声明设置为



Dim p as existingForm
OpenForm(p,existingForm)

函数(P为windows.forms.form,thisForm为*******)
p = NEW thisForm
....

我在*******中放入什么?



Dim p as existingForm


OpenForm(p)


函数(ByRef theForm as existingForm)

结束功能

" Sebastian Santacroce" < SE ****** @ ilogic.com>在消息中写道

news:82 **************************** @ phx.gbl ... < blockquote class =post_quotes>
如果我想将表单(我创建的表单)传递给
函数,我将声明设置为



Dim p as existingForm
OpenForm(p,existingForm)

函数(P为windows.forms.form,thisForm为*******)
p = NEW thisForm
....

我在*******中放入什么?


< br>

Sebastian Santacroce写道:


如果我想将一个表单(我创建的表单)传递给
函数,我将如何设置声明例如

Dim p as existingForm
OpenForm(p,existingForm)

函数(P as windows.forms.form ,这个形式为*******)
p = NEW thisForm
....

我在*******中放什么?




如果我读得正确,你想要一个创建一个ins的函数

类型,不知道该类型的确切类型。因为

..Net还没有泛型(但是),唯一的方法就是使用

反射。请注意,下面的示例将比使用''p =

new existingForm''慢。


首先,我会想到的设计你的程序决定你是否* b $ b *真的*需要这个。但是如果你必须这样做,那就是这样的:


Imports System.Reflection


Public Sub OpenForm(ByVal p As Windows.Forms。 Form,ByVal thisForm As Type)

Dim ci As ConstructorInfo = thisForm.GetConstructor(Type.EmptyTypes)

p = ci.Invoke(Nothing)

''用p做一些事情。

End Sub


Dim p As existingForm

OpenForm(p,Type.GetType( YourNamespace.existingForm))


再次,只有在绝对没有其他选择时才这样做。这是丑陋的代码,

难以维护,性能不高,而且不是类型安全的。


-

Sven Groot

http://unforgiven.bloghorn.com


Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
.....

What do I put in *******?

解决方案

as ExistingForm. If I''m reading this right, you don''t even need to pass the
P.
"Sebastian Santacroce" <se******@ilogic.com> wrote in message
news:82****************************@phx.gbl...

Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?



Dim p as existingForm

OpenForm(p)

Function(ByRef theForm as existingForm)
End Function
"Sebastian Santacroce" <se******@ilogic.com> wrote in message
news:82****************************@phx.gbl...

Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?



Sebastian Santacroce wrote:

Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?



If I read this right, you want to have a function that create an instance of
a type, without knowing a priori exactly what that type will be. Because
..Net doesn''t have generics (yet), the only way to do this is using
reflection. Note that the example below will be slower than just using ''p =
new existingForm''.

First, I''d think the design of your program through to decide if you
*really* need this. But if you must do it, this is how:

Imports System.Reflection

Public Sub OpenForm(ByVal p As Windows.Forms.Form, ByVal thisForm As Type)
Dim ci As ConstructorInfo = thisForm.GetConstructor(Type.EmptyTypes)
p = ci.Invoke(Nothing)
'' do something with p.
End Sub

Dim p As existingForm
OpenForm(p, Type.GetType("YourNamespace.existingForm"))

Again, only do this if you have absolutely no alternative. It is ugly code,
hell to maintain, not very performant, and not type-safe.

--
Sven Groot

http://unforgiven.bloghorn.com


这篇关于在函数中传递表单对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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