循环参考? [英] Circular references ?

查看:43
本文介绍了循环参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何解决VB.NET中循环引用的问题

(对不起我是.NET新手)。

我创建了一个项目它有2个类,MyHandler和MyObject。

MyHandler只需要在另一个项目中调用另一个类,并将一个类型为MyObject的

参数传递给它。

所以不同项目中的类需要导入第一个项目

命名空间,以便知道MyObject是什么。

MyHandler类需要导入第二个项目名称空间,所以它可以在第二个项目中调用该类。


即使Java很容易在.NET中也不允许这样做这个。
这个。


见下面的代码:

(第一个项目:namespace = MyClass.Test1)


公共类MyHandler

公共函数DoSomething(ByVal obj As MyObject)As String

Dim a As New [MyClass] .Test2.MyAction

返回a.DoMethod(obj)



结束函数

结束类


公共类MyObject

私人a,b为字符串

公共财产attrib1()为字符串

获取

返回

结束获取

套装(ByVal价值为字符串)

a =价值

结束套件

结束物业

公共财产attrib2()作为字符串

获取

返回b

结束获取

套装(ByVal价值为字符串)

b =价值

套装

结束物业

结束班级

(第二个项目:名称空间= MyClass.Test2)


进口[MyClass] .Test1


公开类MyAction

公共函数DoMethod(ByVal obj As MyObject)As String

返回obj.attrib1

结束函数

结束课


上面用红色突出显示的行失败了,因为根据.NET它不知道
知道什么类型的val你称之为''obj''就是把它传递到第二个项目中的

方法。

我得到的错误是:


H:\ work\testbed\MyClass.Test1 \ MyHandler.vb(4):需要参考

汇编''MyClass.Test1''包含的类型'' MyClass.Test1.MyObject'。添加

一个到您的项目。


这似乎是一个非常基本的功能,我必须声明

这里的东西不正确


任何想法?

解决方案

" Dave S" <哒******** @ hotmail.com>在消息中写道

news:ac ****************************** @ news.teranew s。 com ...

任何人都知道如何解决VB.NET中循环引用的问题
(对不起我是.NET新手)。
我创建了一个项目其中有2个类,MyHandler和MyObject。
MyHandler只需要在另一个项目中调用另一个类,并将类型为MyObject的
a参数传递给它。
所以不同项目中的类需要导入第一个项目命名空间,以便知道MyObject是什么。
MyHandler类需要导入第二个项目命名空间,以便它可以在第二个项目中调用该类。



通常情况下,通过创建包含

业务类型的第三个项目来解决这个问题。


然后让你的另外两个项目引用这个新项目,以便访问公共类型(所以在你的情况下,将MyObject放在第三个项目中)。

参考 - >添加参考文献 - >项目标签)


Erik


" Dave S" <哒******** @ hotmail.com>在消息中写道

news:ac ****************************** @ news.teranew s。 com ...

任何人都知道如何解决VB.NET中循环引用的问题
(对不起我是.NET新手)。
我创建了一个项目其中有2个类,MyHandler和MyObject。
MyHandler只需要在另一个项目中调用另一个类,并将类型为MyObject的
a参数传递给它。
所以不同项目中的类需要导入第一个项目命名空间,以便知道MyObject是什么。
MyHandler类需要导入第二个项目命名空间,以便它可以在第二个项目中调用该类。



通常情况下,通过创建包含

业务类型的第三个项目来解决这个问题。


然后让你的另外两个项目引用这个新项目,以便访问公共类型(所以在你的情况下,将MyObject放在第三个项目中)。

参考 - >添加参考文献 - >项目标签)


Erik


Erik,


多亏了这一点,我只想确认只用2个项目才能确定它是不可能的。

我试图将我的对象移动到另一个项目中(所以它是排序的一个接口

项目)然后每个其他项目都可以引用该项目

而不会导致循环引用问题。

我只是想到在Java中你可以很容易地做到这一点有点凌乱

没有包含你想要传递的对象的中间人包

这些类。

至少我知道在最初宣布2

项目时,我不会错过任何东西。

将来必须记住这个项目(我一直在努力争取这一切

下午)


戴夫



" ; Erik Frey < ER ******* @ hotmail.com>在消息中写道

news:uC ************** @ TK2MSFTNGP10.phx.gbl ...

" Dave S" <哒******** @ hotmail.com>在消息中写道
新闻:ac ****************************** @ news.teranew s.com ...

任何人都知道如何解决
VB.NET中的循环引用问题(对不起我是.NET新手)。
我创建了一个有2个类的项目在其中,MyHandler和MyObject。
MyHandler只需要在另一个项目中调用另一个类,并且

参数传递给MyObject因此,不同项目中的类需要导入第一个项目命名空间,以便知道MyObject是什么。
MyHandler类需要导入第二个项目命名空间,所以它

可以在第二个项目中调用该类。



通常,通过创建包含您的
业务类型的第三个项目来解决这个问题。

然后让你的另外两个项目参考这个新项目,让



访问co mmon类型(所以在你的情况下,将MyObject放在第三个项目中)。

另外,请记住,即使你在解决方案中有三个项目,你仍然需要添加手动项目参考(右键点击
参考文献 - >添加参考文献 - >项目标签)

Erik



Anyone know how to get round the problem of circular references in VB.NET
(sorry I''m a .NET newbie).
I create one project which has 2 classes in it, MyHandler and MyObject.
MyHandler just needs to call another class in a different project and pass a
parameter of type MyObject to it.
So the class in the different project needs to import the first project
namespace so it know what MyObject is.
The MyHandler class needs to import the second project namespace so it can
call the class in the second project.

This doesn''t seem to be allowed in .NET even though Java could easily do
this.

See code below :
(1st project : namespace = MyClass.Test1)

Public Class MyHandler
Public Function DoSomething(ByVal obj As MyObject) As String
Dim a As New [MyClass].Test2.MyAction

Return a.DoMethod(obj)


End Function
End Class

Public Class MyObject
Private a, b As String
Public Property attrib1() As String
Get
Return a
End Get
Set(ByVal Value As String)
a = Value
End Set
End Property
Public Property attrib2() As String
Get
Return b
End Get
Set(ByVal Value As String)
b = Value
End Set
End Property
End Class
(2nd project : namespace = MyClass.Test2)

Imports [MyClass].Test1

Public Class MyAction
Public Function DoMethod(ByVal obj As MyObject) As String
Return obj.attrib1
End Function
End Class

The line highlighted in red above fails because according to .NET it doesn''t
know what type the value called ''obj'' is when it comes to passing it to the
method in the 2nd project.
The error I get is :

H:\Work\testbed\MyClass.Test1\MyHandler.vb(4): Reference required to
assembly ''MyClass.Test1'' containing the type ''MyClass.Test1.MyObject''. Add
one to your project.


This seems a really basic piece of functionality and I must be declaring
something incorrectly here

Any ideas ??

解决方案

"Dave S" <da********@hotmail.com> wrote in message
news:ac******************************@news.teranew s.com...

Anyone know how to get round the problem of circular references in VB.NET
(sorry I''m a .NET newbie).
I create one project which has 2 classes in it, MyHandler and MyObject.
MyHandler just needs to call another class in a different project and pass a parameter of type MyObject to it.
So the class in the different project needs to import the first project
namespace so it know what MyObject is.
The MyHandler class needs to import the second project namespace so it can
call the class in the second project.



Typically this is adressed by creating a third project that contains your
business types in it.

Then have your other two projects refer to this new project, to have access
to the common type (so in your case, put MyObject in the third project).

Also, keep in mind that even if you have three projects in a solution, you
still have to add the project references manually (right click on
References - > Add References -> Projects tab)

Erik


"Dave S" <da********@hotmail.com> wrote in message
news:ac******************************@news.teranew s.com...

Anyone know how to get round the problem of circular references in VB.NET
(sorry I''m a .NET newbie).
I create one project which has 2 classes in it, MyHandler and MyObject.
MyHandler just needs to call another class in a different project and pass a parameter of type MyObject to it.
So the class in the different project needs to import the first project
namespace so it know what MyObject is.
The MyHandler class needs to import the second project namespace so it can
call the class in the second project.



Typically this is adressed by creating a third project that contains your
business types in it.

Then have your other two projects refer to this new project, to have access
to the common type (so in your case, put MyObject in the third project).

Also, keep in mind that even if you have three projects in a solution, you
still have to add the project references manually (right click on
References - > Add References -> Projects tab)

Erik


Erik,

Thanks for this, I just wanted to confirm that it''s not possible to do it
with just 2 projects.
I tried moving my object into another project (so it''s sort of an interface
project) and then each of the other projects can reference that project
without causing the circular reference problem.
I just thought it was a bit messy when in Java you could easily do this
without a middleman package containing the object you want to pass between
the classes.
At least I know that it wasn''t something I''d missed out when declaring the 2
projects initially.
Will have to remember this one in future (I''ve been struggling with this all
afternoon)

Dave


"Erik Frey" <er*******@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...

"Dave S" <da********@hotmail.com> wrote in message
news:ac******************************@news.teranew s.com...

Anyone know how to get round the problem of circular references in VB.NET (sorry I''m a .NET newbie).
I create one project which has 2 classes in it, MyHandler and MyObject.
MyHandler just needs to call another class in a different project and pass

a

parameter of type MyObject to it.
So the class in the different project needs to import the first project
namespace so it know what MyObject is.
The MyHandler class needs to import the second project namespace so it

can call the class in the second project.



Typically this is adressed by creating a third project that contains your
business types in it.

Then have your other two projects refer to this new project, to have


access to the common type (so in your case, put MyObject in the third project).

Also, keep in mind that even if you have three projects in a solution, you
still have to add the project references manually (right click on
References - > Add References -> Projects tab)

Erik



这篇关于循环参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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