没有不安全指针的引用 [英] References without unsafe pointers

查看:77
本文介绍了没有不安全指针的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


有人可以告诉我如何将ref参数分配给变量

,以便在对此变量进行更新时它们反映在

调用对象的参考参数中。我希望能够在构造函数之外更改引用变量的值

。例如。


private bool loggedIn;


//从另一个表单调用的构造函数

public void LoginForm (ref bool log)

{

loggedIn = log;

}


private void loginIsTrue ()

{

loggedIn = true;

}


我想要''日志''在调用对象中变回为什么

''loggedIn''更改为。


希望这很清楚。

Jesse

Hi Everyone,

Can someone show me how to go about assigning a ref parameter to a variable
so that when updates are made to this variable they are reflected in the
calling object''s reference parameter. I want to be able to change the value
of the referenced variable outside of the constructor. eg.

private bool loggedIn;

//Constructor which is called from another form
public void LoginForm(ref bool log)
{
loggedIn = log;
}

private void loginIsTrue()
{
loggedIn = true;
}

I want the ''log'' variable back in the calling object to be whatever
''loggedIn'' is changed to.

Hope this is clear.
Jesse

推荐答案

好吧,你可以在调用应用程序中设置一个事件处理程序,并且

订阅事件通过您的登录表单。然后,当用户使用

登录表单时,您可以向所有事件处理程序抛出一个事件,他们可以更新他们本地的值副本。


HTH,

---尼克


" Jesse" < JE ***** @ hotmail.com>在消息中写道

新闻:OX ************** @ tk2msftngp13.phx.gbl ...
well, you could set up an event handler in the calling application and
subscribe to events through by your login form. Then, when the user uses
the login form, you could throw an event to all event handlers, who could
update their local copies of the value.

HTH,
--- Nick

"Jesse" <je*****@hotmail.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
大家好,

有人可以告诉我如何将ref参数分配给
变量,这样当对这个变量进行更新时,它们会反映在
调用对象的引用参数中。我希望能够在构造函数之外更改引用变量的
值。例如。

私人bool登录;

//从另一种形式调用的构造函数
public void LoginForm(ref bool log)
{
loggedIn = log;
}
private void loginIsTrue()
{
loggedIn = true;
}

我希望调用对象中的''log''变量变为
''loggedIn''更改为。

希望这很清楚。
Jesse
Hi Everyone,

Can someone show me how to go about assigning a ref parameter to a variable so that when updates are made to this variable they are reflected in the
calling object''s reference parameter. I want to be able to change the value of the referenced variable outside of the constructor. eg.

private bool loggedIn;

//Constructor which is called from another form
public void LoginForm(ref bool log)
{
loggedIn = log;
}

private void loginIsTrue()
{
loggedIn = true;
}

I want the ''log'' variable back in the calling object to be whatever
''loggedIn'' is changed to.

Hope this is clear.
Jesse



Jesse< je ***** @ hotmail.com>写道:
Jesse <je*****@hotmail.com> wrote:
有人可以告诉我如何将ref参数赋值给变量
这样当对这个变量进行更新时它们会反映在
调用对象中的参考参数。我希望能够在构造函数之外更改引用变量的值。
Can someone show me how to go about assigning a ref parameter to a variable
so that when updates are made to this variable they are reflected in the
calling object''s reference parameter. I want to be able to change the value
of the referenced variable outside of the constructor.




你不能,谢天谢地。它可能会产生可怕的代码,可能的私有变量的价值

以非显而易见的方式在课外改变等等。另外考虑

,在第二次

通话时,该变量甚至可能不存在*


你*可以*做什么创建一个包含

布尔值的引用类型。将其传递给您的方法并保留对它的引用,然后

然后更改布尔值inside的值。以后的类型。


-

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

如果回复该组,请不要给我发邮件



You can''t, thank goodness. It would make for horrendously
unmaintainable code, with the values of potentially private variables
being changed outside the class in non-obvious ways, etc. Also consider
that the variable may not even *exist* any more by the time the second
call is made.

What you *could* do is create a reference type which contains a
boolean. Pass that into your method and keep a reference to it, and
then change the value of the boolean "inside" the type at a later date.

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


Hey Jon&尼克,


感谢您的反馈。


我想要做的是拥有一个可重复使用的loginForm。单击

登录按钮时,单击此loginForm,将检查用户的有效性,如果一切正常,则调用表单(主窗体)
将具有布尔值如果用户是/ b $ b登录或不需要更新。如果不能更新这个真实或错误的父表格,你怎么能有一个可重复使用的

loginForm?


我认为尼克有一个好的阅读它。


Jon你能解释一下你的意思(可能还有例子)

关于:

您*可以*做的是创建一个引用类型。

我该怎么做?


你的意思是:

bool b;

b = new Boolean();


包含一个布尔值。将其传递给您的方法并保留一个引用

,然后更改布尔值inside的值。这个类型稍后

日期。


对不起,如果这一切对您来说都显得微不足道。

Jesse


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Hey Jon & Nick,

Thanks for the feedback.

What i want to do is have a reuseable loginForm. When the login button on
this loginForm is clicked, the users validility is checked and if all is ok,
the calling form (main form), which will have a boolean for if the user is
logged on or not needs to be updated. How else can you have a reuseable
loginForm if not to be able to update the parent form of this true or false?

I think nick has a good read on it.

Jon could you explain what you mean a bit further (possibly with example)
with regard to:

What you *could* do is create a reference type.
How do i do that?

do you mean:
bool b;
b = new Boolean();

which contains a boolean. Pass that into your method and keep a reference
to it, and then change the value of the boolean "inside" the type at a later
date.

Sorry, if this all seems trivial to you.
Jesse

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jesse< je ***** @ hotmail.com>写道:
Jesse <je*****@hotmail.com> wrote:
有人可以告诉我如何将ref参数分配给
变量,这样当对这个变量进行更新时,它们会反映在
调用对象中的参考参数。我希望能够在构造函数之外更改引用变量的
值。
Can someone show me how to go about assigning a ref parameter to a variable so that when updates are made to this variable they are reflected in the
calling object''s reference parameter. I want to be able to change the value of the referenced variable outside of the constructor.



你不能,谢天谢地。它会产生可怕的无法维护的代码,潜在的私有变量的值会以非显而易见的方式在类外转换,等等。还要考虑变量甚至可能不存在*存在*在第二次打电话时再做任何事情。

你*可以*做的是创建一个包含
布尔值的引用类型。将其传递给您的方法并保持对它的引用,然后
然后更改布尔值inside的值。以后的类型。

- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



You can''t, thank goodness. It would make for horrendously
unmaintainable code, with the values of potentially private variables
being changed outside the class in non-obvious ways, etc. Also consider
that the variable may not even *exist* any more by the time the second
call is made.

What you *could* do is create a reference type which contains a
boolean. Pass that into your method and keep a reference to it, and
then change the value of the boolean "inside" the type at a later date.

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



这篇关于没有不安全指针的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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