要求从一个自定义对象转换为另一个自定义对象 [英] requirement to cast from one custom object to another custom object

查看:82
本文介绍了要求从一个自定义对象转换为另一个自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个自定义vb.net类,其中80%的属性都相似

并且有一种方法具有匹配的签名,我可以在一个之间施放

和另一个?我需要让每个班级实现一个通用的界面吗?


谢谢,


craig buchanan

解决方案

否和是。


您应该定义一个接口或一个公共基类(相同的概念

真的)。然后你可以通过引用

来访问基类的派生类方法。


Craig Buchanan <所以***** @ somewhere.com>在消息中写道

news:eQ **************** @ TK2MSFTNGP11.phx.gbl ...

如果我有两个自定义的vb.net类,其中80%的属性都相似
并且有一种方法具有匹配的签名,我可以在一个
和另一个之间进行投射吗?我是否需要让每个班级实现一个通用界面?

谢谢,





" Craig Buchanan" <所以***** @ somewhere.com>在消息中写道

news:eQ **************** @ TK2MSFTNGP11.phx.gbl ...

如果我有两个自定义的vb.net类,其中80%的属性都相似
并且有一种方法具有匹配的签名,我可以在一个
和另一个之间进行投射吗?我是否需要让每个类实现一个通用接口?


是的......或者它们是从同一个父级继承的...在这种情况下那些常见的

子方法会覆盖(sp?)方法,然后你可以将
强制转换为父类型,但不能直接在类型之间。


界面也不错......

谢谢,

blockquote>


< blockquote> Craig,


当心!!!


您可以选择基类/接口路由,但这是正确的

架构决策。这两个类真的是两个子类型的另一个,更通用的东西(继承)吗?或者它们是否都符合相同的

合约(接口)?


相反,你可以创建某种知识的观察者/检查员类

如何从两个班级获得所需信息。如果没有要求继承/接口实现,这将使你的

类保持纯净。


只是想一想,

Andrew J. Marshall

MCP(Visual Basic)

弗吉尼亚州费尔法克斯


" Craig Buchanan" <所以***** @ somewhere.com>在消息中写道

news:eQ **************** @ TK2MSFTNGP11.phx.gbl ...

如果我有两个自定义的vb.net类,其中80%的属性都相似
并且有一种方法具有匹配的签名,我可以在一个
和另一个之间进行投射吗?我是否需要让每个班级实现一个通用界面?

谢谢,



If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?

thanks,

craig buchanan

解决方案

No and Yes.

You should either define an interface or a common base class (same concept
really). Then you can access the derived class methods through references
to the base class.

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...

If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?

thanks,

craig buchanan




"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...

If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?

Yep... or they inherit from the same parent... In which case those common
methods would be overridden (sp?) by the child classes, and then you could
cast to the parent type, but not directly between the types.

Interface would be fine too...
thanks,

craig buchanan



Craig,

BEWARE!!!

You could go the base class/interface route, but is that the "correct"
architectural decision. Are these two classes REALLY two subtypes of
another, more generic thing (inheritance)? Or do they both conform the same
contract (interfaces)?

Instead, you could create some sort of observer/inspector class that knows
how get the required information from your two classes. This keeps your
classes pure if inheritance/interface implementation it not called for.

Just a thought,
Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...

If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?

thanks,

craig buchanan



这篇关于要求从一个自定义对象转换为另一个自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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