“ref object a” <> “ByRef a As Object” ??? [英] "ref object a" <> "ByRef a As Object" ???

查看:44
本文介绍了“ref object a” <> “ByRef a As Object” ???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GDay all,


我觉得奇怪......

我写了一个简单的C#函数


public void bind(ref object a,ref object b,bool atob)

{

if(atob)

b = a;

其他

a = b;

}


我从windows应用程序调用它。


string data =" moo";

bind(ref(object)this.Text,ref(object)data,false);


正如我所料,我得到错误CS1510参考或出局参数必须是

左值。

这是我没有做的'我期待,当我写一个vb.net等价物时:


公共子绑定(ByRef a As Object,ByRef b As Object,ByVal atob As

布尔)

如果(atob)那么

b = a

否则

a = b

结束如果

结束子


并从Windows应用程序调用它:


Dim data As String =" moo"

bind(Me.Text,data,False)


它不仅编译它还执行和工作 - 表格标题

改为moo。


为什么VB.NET不会吐出假人吗?

是否可以哄骗C#做任何vb做的事情?


-DM

解决方案

Duck博士< sh ******** @ optusnet.com.au>写道:


< snip>

这是我没想到的,当我写一个vb.net等效时:<公共子绑定(ByRef a As Object,ByRef b As Object,ByVal atob As
Boolean)
if(atob)那么
b = a
否则
a = b
结束如果


并从Windows应用程序调用它:

Dim data As String =" ; moo"
绑定(Me.Text,数据,错误)

不仅编译它还执行和工作 - 表单标题
被更改为moo 。

为什么VB.NET不会吐出假人?
是否可以哄骗C#做任何vb做的事?




我已经了解了VB.NET的内容,基本上它是

创建临时对象,通过引用传递它们,然后设置

数据和Me.Text后来的结果。非常可怕,如果你问b $ b请问我......但只有当参数是

属性或形式参数类型的子类型时才会这样做。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要邮寄我也是


谢谢伴侣


我同意这个后期绑定的临时对象 [组成术语]事情听起来很可怕


我看不到背后的理性


这听起来会引起很多多线程应用程序中的问题

- 函数调用退出后不能识别信号量的赋值..

- 一个进程与另一个进程通信它修改了引用的对象参数

但修改并不明显,因为该过程尚未退出...


-DM

你好Jon,


昨天我很惊讶你在线程C#学习者提供了

建议去寻找VB.net,我从来没有想到过,但是你会看到事情可以改变的事情。 (不需要评论,我很可能已经知道了)


:-)


但是当这条消息以新闻组语言出现时。 VB它会

得到很多评论。


1.传递by ref来自一个对象,你有没有尝试过让我参加关于拳击的讨论?

2.作为对象传递

3不是Option Strict On。


我明白这是一个理论上的问题,但是我确实想要加上

这个,因为我没有看到它你的评论,而这个程序将运行

可能是它的10倍。


当我忘记时,你让我总是注意什么?实例到

Xor有点并做一个if相反。


(这部分不是我喜欢的地方)


:-)


Cor

公共子绑定(ByRef a As Object,ByRef b As Object,ByVal atob As
Boolean)
如果(atob)那么
b = a
等等a = b
结束如果
End Sub



GDay all,

Something seems odd to me....
I wrote a simple C# function

public void bind(ref object a, ref object b, bool atob)
{
if(atob)
b = a;
else
a = b;
}

I call it from a windows app.

string data = "moo";
bind(ref (object)this.Text, ref (object)data, false);

As I expected I got Error CS1510 "A ref or out argument must be an
lvalue".
Here is what I didnt''t expect, when I wrote a vb.net equivilant:

Public Sub bind(ByRef a As Object, ByRef b As Object, ByVal atob As
Boolean)
If (atob) Then
b = a
Else
a = b
End If
End Sub

and call it from a windows app:

Dim data As String = "moo"
bind(Me.Text, data, False)

Not only did it compile it also executed and worked - the forms title
was changed to "moo".

Why does VB.NET not spit the dummy?
Is it possible to coax C# into doing whatever vb did?

-DM

解决方案

Dr Duck <sh********@optusnet.com.au> wrote:

<snip>

Here is what I didnt''t expect, when I wrote a vb.net equivilant:

Public Sub bind(ByRef a As Object, ByRef b As Object, ByVal atob As
Boolean)
If (atob) Then
b = a
Else
a = b
End If
End Sub

and call it from a windows app:

Dim data As String = "moo"
bind(Me.Text, data, False)

Not only did it compile it also executed and worked - the forms title
was changed to "moo".

Why does VB.NET not spit the dummy?
Is it possible to coax C# into doing whatever vb did?



I''ve had a look at what VB.NET does under the covers, and basically it
creates temporary objects, passes those by reference, and then sets
data and Me.Text to the results afterwards. Utterly horrible, if you
ask me... but it only does it that way if the parameters are either
properties or subtypes of the formal parameter types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Thanks Mate

I agree this "late bound tempory object" [made up term] thing sounds Horrible

I can''t see the rational behind it

It sounds like it would cause a lot of problems in a multi threaded app
- assignment after the function call exits not being semaphore aware..
- a process comunicating to another it had modified the object referenced as a parameter
but the modification is not apparent as the process has not exited yet...

-DM


Hi Jon,

I was yesterday suprissed reading that you did in the thread C# learner give
an advice to look for VB.net, that I never did expect, however you sees how
things can change. (no comments needed, I most probably know them already)

:-)

However when this message would come in the newsgroup languages.VB it would
get a lot of comments.

1. passing "by ref" from an Object, did you not try once to get me in a
discussion about boxing?.
2. passing as an Object
3. not Option Strict On.

I understand that it is a theoretical question, however I did want to add
this, because I did not see it in your comment, while this program will run
probably 10 times as slow as it could be.

What is something you make me always attent on when I forget by instance to
Xor a bit and do an "if" instead.

(And this part is not the area I like)

:-)

Cor

Public Sub bind(ByRef a As Object, ByRef b As Object, ByVal atob As
Boolean)
If (atob) Then
b = a
Else
a = b
End If
End Sub



这篇关于“ref object a” &LT;&GT; “ByRef a As Object” ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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