如何投射表格 [英] How to cast a Form
问题描述
我有两种形式。每个都包含一个属性,比如道具。
我做
Public FormBeing用作表格
..
..
..
然后我做
FormBeingUsed = Form1
或者
FormBeingUsed = Form2
..
..
..
然后我想做
FormBeingUsed.Prop = 123
当然,因为Form没有属性Prop编译器抱怨
有没有一个好办法呢?
谢谢
I have two forms. Each contains a property, say Prop.
I do
Public FormBeingUsed as Form
..
..
..
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
..
..
..
then I want to do
FormBeingUsed.Prop=123
Of course, because Form does not have a property Prop the compiler complains
Is there a good way to do this?
Thanks
推荐答案
Just Me,
我有两种形式。每个都包含一个属性,比如道具。
有没有一个好方法可以做到这一点?
多态性,OO的租户之一,是一个很好的方法。你可以使用类继承或接口实现来实现这个
多态性。
''类继承
公共类BaseForm
继承System.Windows.Forms.Form
Public Property Prop As Integer
...
结束班
公共类Form1
继承BaseForm
...
结束班
公共类Form2
继承BaseForm
...
结束班
Public FormBeing用作BaseForm
FormBeingUsed = Form1
FormBeingUsed.Prop = 123
FormBeingUsed = Form2
FormBeingUsed.Prop = 123
''界面实施
Public Interface IProp
Public Property Prop As Integer
...
结束界面
公共类Form1
继承System.Windows.Forms.Form
Implem ents IProp
公共财产支柱作为整数实施IProp.Prop
...
结束班
公共类Form2
继承System.Windows.Forms.Form
实现IProp
公共财产支柱作为整数实现IProp.Prop
...
结束班级
Public FormBeing用作IProp
>
FormBeingUsed = Form1
FormBeingUsed.Prop = 123
FormBeingUsed = Form2
FormBeingUsed.Prop = 123
您可以在BaseForm中将Prop定义为Overridable,或者定义一个
OnPropChanged方法,当值
更改时,BaseForm.Prop.Set方法调用该方法如果你需要派生形式来了解Prop
更改的价值...
我赞成通过接口实现继承类继承通常
类继承将产生更少的代码。
希望这有助于
Jay
>
"只是我 < GR **** @ a-znet.com>在留言中写道
新闻:u1 ************** @ TK2MSFTNGP09.phx.gbl ...我有两种形式。每个都包含一个属性,比如道具。
我做公共FormBeing用作表格
。
。
。
然后我做
或者
FormBeingUsed = Form2
。
。
。
然后我想做
FormBeingUsed.Prop = 123
当然,因为Form没有属性Prop编译器抱怨
有没有好办法呢?
谢谢
I have two forms. Each contains a property, say Prop.
Is there a good way to do this?
Polymorphism, one of the tenants of OO, is a good way to do this. You can
use either Class Inheritance or Interface Implementation to achieve this
Polymorphism.
'' Class Inheritance
Public Class BaseForm
Inherits System.Windows.Forms.Form
Public Property Prop As Integer
...
End Class
Public Class Form1
Inherits BaseForm
...
End Class
Public Class Form2
Inherits BaseForm
...
End Class
Public FormBeingUsed as BaseForm
FormBeingUsed= Form1
FormBeingUsed.Prop=123
FormBeingUsed=Form2
FormBeingUsed.Prop=123
'' Interface Implementation
Public Interface IProp
Public Property Prop As Integer
...
End Interface
Public Class Form1
Inherits System.Windows.Forms.Form
Implements IProp
Public Property Prop As Integer Implements IProp.Prop
...
End Class
Public Class Form2
Inherits System.Windows.Forms.Form
Implements IProp
Public Property Prop As Integer Implements IProp.Prop
...
End Class
Public FormBeingUsed as IProp
FormBeingUsed= Form1
FormBeingUsed.Prop=123
FormBeingUsed=Form2
FormBeingUsed.Prop=123
You could define Prop to be Overridable in BaseForm or define an
OnPropChanged method that the BaseForm.Prop.Set method calls when the value
changes if you need derived forms to know about the value of Prop
changing...
I would favor Class Inheritance over Interface Implementation as normally
Class Inheritance will produce less code.
Hope this helps
Jay
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...I have two forms. Each contains a property, say Prop.
I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123
Of course, because Form does not have a property Prop the compiler
complains
Is there a good way to do this?
Thanks
"只是我 < GR **** @ a-znet.com> schrieb:
" Just Me" <gr****@a-znet.com> schrieb:
我有两种形式。每个都包含一个属性,比如道具。
我做公共FormBeing用作表格
。
。
。
然后我做
或者
FormBeingUsed = Form2
。
。
。
然后我想做
FormBeingUsed.Prop = 123
当然,因为Form没有属性Prop编译器抱怨
有没有好办法呢?
I have two forms. Each contains a property, say Prop.
I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123
Of course, because Form does not have a property Prop the compiler
complains
Is there a good way to do this?
\\\
公共接口IUsable
用作布尔值的属性
结束接口
公共类Form1
继承表格
实施IUsable
公共财产使用( )作为布尔实现IUsable.Used
...
结束财产
...
结束类
公共舱表格2
...
结束班级
..
..
..
Dim f As IUsable
如果......那么
f =新Form1()
Else
f =新Form2()
结束如果
f.Used = True
/ //
另一种方法是使用一个公共基类,它将一个''Used''属性
添加到表单类中,并从中添加''Form1' '和''Form2''继承。
-
MS Herfried K. Wagner
MVP< URL:http ://dotnet.mvps.org/>
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>
\\\
Public Interface IUsable
Property Used As Boolean
End Interface
Public Class Form1
Inherits Form
Implements IUsable
Public Property Used() As Boolean Implements IUsable.Used
...
End Property
...
End Class
Public Class Form2
...
End Class
..
..
..
Dim f As IUsable
If...Then
f = New Form1()
Else
f = New Form2()
End If
f.Used = True
///
Another approach would use a common base class that adds a ''Used'' property
to the form class and from which ''Form1'' and ''Form2'' inherit.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Just Me,
我有两种形式。每个都包含一个属性,比如道具。
有没有一个好方法可以做到这一点?
多态性,OO的租户之一,是一个很好的方法。你可以使用类继承或接口实现来实现这个
多态性。
''类继承
公共类BaseForm
继承System.Windows.Forms.Form
Public Property Prop As Integer
...
结束班
公共类Form1
继承BaseForm
...
结束班
公共类Form2
继承BaseForm
...
结束班
Public FormBeing用作BaseForm
FormBeingUsed = Form1
FormBeingUsed.Prop = 123
FormBeingUsed = Form2
FormBeingUsed.Prop = 123
''界面实施
Public Interface IProp
Public Property Prop As Integer
...
结束界面
公共类Form1
继承System.Windows.Forms.Form
Implem ents IProp
公共财产支柱作为整数实施IProp.Prop
...
结束班
公共类Form2
继承System.Windows.Forms.Form
实现IProp
公共财产支柱作为整数实现IProp.Prop
...
结束班级
Public FormBeing用作IProp
>
FormBeingUsed = Form1
FormBeingUsed.Prop = 123
FormBeingUsed = Form2
FormBeingUsed.Prop = 123
您可以在BaseForm中将Prop定义为Overridable,或者定义一个
OnPropChanged方法,当值
更改时,BaseForm.Prop.Set方法调用该方法如果你需要派生形式来了解Prop
更改的价值...
我赞成通过接口实现继承类继承通常
类继承将产生更少的代码。
希望这有助于
Jay
>
"只是我 < GR **** @ a-znet.com>在留言中写道
新闻:u1 ************** @ TK2MSFTNGP09.phx.gbl ...我有两种形式。每个都包含一个属性,比如道具。
我做公共FormBeing用作表格
。
。
。
然后我做
或者
FormBeingUsed = Form2
。
。
。
然后我想做
FormBeingUsed.Prop = 123
当然,因为Form没有属性Prop编译器抱怨
有没有好办法呢?
谢谢
I have two forms. Each contains a property, say Prop.
Is there a good way to do this?
Polymorphism, one of the tenants of OO, is a good way to do this. You can
use either Class Inheritance or Interface Implementation to achieve this
Polymorphism.
'' Class Inheritance
Public Class BaseForm
Inherits System.Windows.Forms.Form
Public Property Prop As Integer
...
End Class
Public Class Form1
Inherits BaseForm
...
End Class
Public Class Form2
Inherits BaseForm
...
End Class
Public FormBeingUsed as BaseForm
FormBeingUsed= Form1
FormBeingUsed.Prop=123
FormBeingUsed=Form2
FormBeingUsed.Prop=123
'' Interface Implementation
Public Interface IProp
Public Property Prop As Integer
...
End Interface
Public Class Form1
Inherits System.Windows.Forms.Form
Implements IProp
Public Property Prop As Integer Implements IProp.Prop
...
End Class
Public Class Form2
Inherits System.Windows.Forms.Form
Implements IProp
Public Property Prop As Integer Implements IProp.Prop
...
End Class
Public FormBeingUsed as IProp
FormBeingUsed= Form1
FormBeingUsed.Prop=123
FormBeingUsed=Form2
FormBeingUsed.Prop=123
You could define Prop to be Overridable in BaseForm or define an
OnPropChanged method that the BaseForm.Prop.Set method calls when the value
changes if you need derived forms to know about the value of Prop
changing...
I would favor Class Inheritance over Interface Implementation as normally
Class Inheritance will produce less code.
Hope this helps
Jay
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...I have two forms. Each contains a property, say Prop.
I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123
Of course, because Form does not have a property Prop the compiler
complains
Is there a good way to do this?
Thanks
这篇关于如何投射表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!