存储对值类型的引用 [英] Storing a reference to a value type

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

问题描述

Hello Group,


我在我的程序中使用了这样的东西:


公共类WorkObject


private _myproperty as string


公共属性MyProperty as string()

get,set trivial

end物业


public sub new(byval mp as string)

MyProperty = mp

end sub

end class


公共类工作


private _refWorkObject as new WorkObject(String.empty)


public sub new(ByRef wo as WorkObject)

wo = _refWorkObject

DoStuff()

end sub


private sub DoStuff()

''在真实程序中,DoStuff()在某个用户之后被调用

互动

''而不是像这个简化示例中的构造函数。

''所以我不能将wo作为参数传递,我必须将其存储为私有

变量


_refWorkObject.MyP roperty =" new value"

end sub


end class


公共类主要


public sub main()

dim IWantThisToChange as WorkObject

dim w as new Work(IWantThisToChange)


system.console.out.writeline(IWantThisToChange.MyP roperty)''结果

innew value


end sub


结束班

现在,只要我使用公共

字段/的引用类型,所有这一切都可以正常工作属性(在这种情况下是工作对象)。如果我用

System.Guid而不是WorkObject尝试相同的东西,它显然不会工作。我真的有

来包装它,或者在VB.NET中有一些像语法一样的时髦指针,我只是还没有找到



我想要一些类似


公共课的工作


私人_refuid作为新Guid()


public sub new(ByRef uid as Guid)

uid = _refuid

DoStuff()

end sub


private sub DoStuff()

_refuid = getdesiredvalue()''使用传递给

的变量工作构造函数_refuid的新价值


end sub


end class

我们非常感谢任何帮助。


Hello Group,

I used something like this in a program of mine:

public class WorkObject

private _myproperty as string

public property MyProperty as string()
get,set trivial
end property

public sub new(byval mp as string)
MyProperty = mp
end sub

end class

public class Work

private _refWorkObject as new WorkObject(String.empty)

public sub new(ByRef wo as WorkObject)
wo = _refWorkObject
DoStuff()
end sub

private sub DoStuff()
''In the real program, DoStuff() gets called after some user
interaction
''and not by the constructor like in this simplified example.
''So I cant pass wo as a parameter, I have to store it as a private
variable

_refWorkObject.MyProperty = "new value"
end sub

end class

public class main

public sub main()

dim IWantThisToChange as WorkObject
dim w as new Work(IWantThisToChange)

system.console.out.writeline(IWantThisToChange.MyP roperty) ''results
in "new value"

end sub

end class
Now, all of this works fine as long as I use a reference type with public
fields / properties (workobject in this case). If I try the same thing with
System.Guid instead of WorkObject, it obviously won''t work. Do I really have
to wrap it, or is there some funky pointer like syntax in VB.NET that I just
haven''t found yet.

I want something like

public class Work

private _refuid as new Guid()

public sub new(ByRef uid as Guid)
uid = _refuid
DoStuff()
end sub

private sub DoStuff()
_refuid = getdesiredvalue() ''With the variable that was passed into
the work constructor taking the new value of _refuid

end sub

end class
Any help is highly appreciated.


推荐答案

也许我错过了一些东西但是。在这个例子中,引用通过构造函数成功传递



//按钮

Private Sub Button1_Click(ByVal sender As System。对象,ByVal e As

System.EventArgs)处理Button1.Click

Dim g As Guid


Dim mc作为新工作(g)


Debug.WriteLine(g.ToString)

结束子

// CLASS

公共班级工作


私人_refuid为Guid =新Guid(10001000100010001000100010001000)


Public Sub New(ByRef uid As Guid )


uid = _refuid

Debug.WriteLine(_refuid.ToString)


End Sub


Private Sub DoStuff()


End Sub


结束班

/ / OUTPUT


10001000-1000-1000-1000-100010001000

10001000-1000-1000-1000-100010001000


-


OHM(特里伯恩斯)

。 。 。单手人。 。

如果你需要我的电子邮件,请问我


当你不知道自己在做什么时,过得很快


" Jonas Pohlandt" < J。****************** @ dbit-systems.de>在消息中写道

news:eo ************** @ TK2MSFTNGP15.phx.gbl ...
Perhaps I missed something but. In this example, the reference is passed
back via the constructor successfully.
//BUTTON
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim g As Guid

Dim mc As New Work(g)

Debug.WriteLine(g.ToString)
End Sub
//CLASS
Public Class Work

Private _refuid As Guid = New Guid("10001000100010001000100010001000")

Public Sub New(ByRef uid As Guid)

uid = _refuid
Debug.WriteLine(_refuid.ToString)

End Sub

Private Sub DoStuff()

End Sub

End Class
//OUTPUT

10001000-1000-1000-1000-100010001000
10001000-1000-1000-1000-100010001000

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don''t know what you''re doing

"Jonas Pohlandt" <j.******************@dbit-systems.de> wrote in message
news:eo**************@TK2MSFTNGP15.phx.gbl...
Hello Group,

我在我的程序中使用了这样的东西:

公共类WorkObject

私有_myproperty as string

公共财产MyProperty as string()
获取,设置琐碎
最终属性

public sub new(byval mp as string)
MyProperty = mp
end sub <公共类工作

私有_refWorkObject作为新的WorkObject(String.empty)

public sub new( ByRef wo as WorkObject)
wo = _refWorkObject
DoStuff()
end sub

private sub DoStuff()
''在实际程序中,DoStuff ()在一些用户交互之后被调用
''而不是像这个简化的例子那样由构造函数调用。
''所以我不能通过wo作为参数,我有o将其存储为私有变量

_refWorkObject.MyProperty =" new value"
end sub

end class

公共子主要()
暗淡IWantThisToChange作为WorkObject
昏暗作为新工作(IWantThisToChange)

system.console.out.writeline(IWantThisToChange.MyP roperty)
''结果为新值

end sub

结束类

现在,只要我使用带有公共字段/属性的引用类型(在本例中为workobject),所有这些都可以正常工作。如果我用System.Guid而不是WorkObject尝试同样的
,它显然不会工作。我真的只需要b $ b包装它,或者在VB.NET中有一些类似于语法的时髦指针,我还没有找到它的b $ b。

我想要类似的东西

公共课工作

私人_refuid为新Guid()

public sub new(ByRef uid as Guid)
uid = _refuid
DoStuff()
结束sub

私有子DoStuff()
_refuid = getdesiredvalue()''将
传递给工作的变量构造函数采用_refuid的新值

结束类

任何帮助都非常感谢。
Hello Group,

I used something like this in a program of mine:

public class WorkObject

private _myproperty as string

public property MyProperty as string()
get,set trivial
end property

public sub new(byval mp as string)
MyProperty = mp
end sub

end class

public class Work

private _refWorkObject as new WorkObject(String.empty)

public sub new(ByRef wo as WorkObject)
wo = _refWorkObject
DoStuff()
end sub

private sub DoStuff()
''In the real program, DoStuff() gets called after some user
interaction
''and not by the constructor like in this simplified example.
''So I cant pass wo as a parameter, I have to store it as a private
variable

_refWorkObject.MyProperty = "new value"
end sub

end class

public class main

public sub main()

dim IWantThisToChange as WorkObject
dim w as new Work(IWantThisToChange)

system.console.out.writeline(IWantThisToChange.MyP roperty) ''results in "new value"

end sub

end class
Now, all of this works fine as long as I use a reference type with public
fields / properties (workobject in this case). If I try the same thing with System.Guid instead of WorkObject, it obviously won''t work. Do I really have to wrap it, or is there some funky pointer like syntax in VB.NET that I just haven''t found yet.

I want something like

public class Work

private _refuid as new Guid()

public sub new(ByRef uid as Guid)
uid = _refuid
DoStuff()
end sub

private sub DoStuff()
_refuid = getdesiredvalue() ''With the variable that was passed into the work constructor taking the new value of _refuid

end sub

end class
Any help is highly appreciated.



也许我错过了一些东西但是。在这个例子中,引用通过构造函数成功传递



//按钮

Private Sub Button1_Click(ByVal sender As System。对象,ByVal e As

System.EventArgs)处理Button1.Click

Dim g As Guid


Dim mc作为新工作(g)


Debug.WriteLine(g.ToString)

结束子

// CLASS

公共班级工作


私人_refuid为Guid =新Guid(10001000100010001000100010001000)


Public Sub New(ByRef uid As Guid )


uid = _refuid

Debug.WriteLine(_refuid.ToString)


End Sub


Private Sub DoStuff()


End Sub


结束班

/ / OUTPUT


10001000-1000-1000-1000-100010001000

10001000-1000-1000-1000-100010001000


-


OHM(特里伯恩斯)

。 。 。单手人。 。

如果你需要我的电子邮件,请问我


当你不知道自己在做什么时,过得很快


" Jonas Pohlandt" < J。****************** @ dbit-systems.de>在消息中写道

news:eo ************** @ TK2MSFTNGP15.phx.gbl ...
Perhaps I missed something but. In this example, the reference is passed
back via the constructor successfully.
//BUTTON
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim g As Guid

Dim mc As New Work(g)

Debug.WriteLine(g.ToString)
End Sub
//CLASS
Public Class Work

Private _refuid As Guid = New Guid("10001000100010001000100010001000")

Public Sub New(ByRef uid As Guid)

uid = _refuid
Debug.WriteLine(_refuid.ToString)

End Sub

Private Sub DoStuff()

End Sub

End Class
//OUTPUT

10001000-1000-1000-1000-100010001000
10001000-1000-1000-1000-100010001000

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don''t know what you''re doing

"Jonas Pohlandt" <j.******************@dbit-systems.de> wrote in message
news:eo**************@TK2MSFTNGP15.phx.gbl...
Hello Group,

我在我的程序中使用了这样的东西:

公共类WorkObject

私有_myproperty as string

公共财产MyProperty as string()
获取,设置琐碎
最终属性

public sub new(byval mp as string)
MyProperty = mp
end sub <公共类工作

私有_refWorkObject作为新的WorkObject(String.empty)

public sub new( ByRef wo as WorkObject)
wo = _refWorkObject
DoStuff()
end sub

private sub DoStuff()
''在实际程序中,DoStuff ()在一些用户交互之后被调用
''而不是像这个简化的例子那样由构造函数调用。
''所以我不能通过wo作为参数,我有o将其存储为私有变量

_refWorkObject.MyProperty =" new value"
end sub

end class

公共子主要()
暗淡IWantThisToChange作为WorkObject
昏暗作为新工作(IWantThisToChange)

system.console.out.writeline(IWantThisToChange.MyP roperty)
''结果为新值

end sub

结束类

现在,只要我使用带有公共字段/属性的引用类型(在本例中为workobject),所有这些都可以正常工作。如果我用System.Guid而不是WorkObject尝试同样的
,它显然不会工作。我真的只需要b $ b包装它,或者在VB.NET中有一些类似于语法的时髦指针,我还没有找到它的b $ b。

我想要类似的东西

公共课工作

私人_refuid为新Guid()

public sub new(ByRef uid as Guid)
uid = _refuid
DoStuff()
结束sub

私有子DoStuff()
_refuid = getdesiredvalue()''将
传递给工作的变量构造函数采用_refuid的新值

结束类

任何帮助都非常感谢。
Hello Group,

I used something like this in a program of mine:

public class WorkObject

private _myproperty as string

public property MyProperty as string()
get,set trivial
end property

public sub new(byval mp as string)
MyProperty = mp
end sub

end class

public class Work

private _refWorkObject as new WorkObject(String.empty)

public sub new(ByRef wo as WorkObject)
wo = _refWorkObject
DoStuff()
end sub

private sub DoStuff()
''In the real program, DoStuff() gets called after some user
interaction
''and not by the constructor like in this simplified example.
''So I cant pass wo as a parameter, I have to store it as a private
variable

_refWorkObject.MyProperty = "new value"
end sub

end class

public class main

public sub main()

dim IWantThisToChange as WorkObject
dim w as new Work(IWantThisToChange)

system.console.out.writeline(IWantThisToChange.MyP roperty) ''results in "new value"

end sub

end class
Now, all of this works fine as long as I use a reference type with public
fields / properties (workobject in this case). If I try the same thing with System.Guid instead of WorkObject, it obviously won''t work. Do I really have to wrap it, or is there some funky pointer like syntax in VB.NET that I just haven''t found yet.

I want something like

public class Work

private _refuid as new Guid()

public sub new(ByRef uid as Guid)
uid = _refuid
DoStuff()
end sub

private sub DoStuff()
_refuid = getdesiredvalue() ''With the variable that was passed into the work constructor taking the new value of _refuid

end sub

end class
Any help is highly appreciated.



嘿特里,


关键点是,我不能在构造函数中做到这一点。通过它我的意思是

设置uid的值。只是因为当

构造函数被调用时我不知道它。


它必须在DoStuff()子程序中完成,在

构造函数之后的某个时间调用。

在实际程序中,工作类是Windows窗体(或者更确切地说是子窗格

)。

在一些用户交互之后调用DoStuff子。所以你看到我的问题

是,那个

我必须存储构造函数参数uid。不知怎的,所以

dostuff可以指定

它所需的值。在C / C ++中,我可以使用一个指针变量来持有

对Guid

的引用,但我不知道如何在VB.NET中这样做。
Hey Terry,

the crucial point is, I cant do it in the constructor. By "it" i mean
setting the value of the uid. Simply because I don''t know it by the time the
constructor gets called.

It has to be done in the DoStuff() sub, which is called sometime after the
constructor.
In the real program, the work class is a Windows Form (or rather a subclass
of that).
The DoStuff sub is called after some user interaction. So you see my problem
is, that
I have to store the constructor paramter reference "uid" somehow, so that
dostuff can assign
it the desired value. In C/C++ I could just use a pointer variable to hold
the reference to the Guid
but I don''t know how to do that in VB.NET.
也许我错过了一些东西但是。在这个例子中,引用通过构造函数成功传递回来。

//按钮
私有子按钮1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim g As Guid

Dim mc作为新工作(g)

Debug.WriteLine(g.ToString)<结束Sub

// CLASS
公共课工作

私人_refuid为Guid = New Guid(10001000100010001000100010001000)

/> Public Sub New(ByRef uid As Guid)

uid = _refuid
Debug.WriteLine(_refuid.ToString)

End Sub

Private Sub DoStuff()

End Sub

End Class

//输出

10001000-1000- 1000-1000-100010001000
10001000-1000-1000-1000-100010001000
Perhaps I missed something but. In this example, the reference is passed
back via the constructor successfully.

//BUTTON
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim g As Guid

Dim mc As New Work(g)

Debug.WriteLine(g.ToString)
End Sub
//CLASS
Public Class Work

Private _refuid As Guid = New Guid("10001000100010001000100010001000")

Public Sub New(ByRef uid As Guid)

uid = _refuid
Debug.WriteLine(_refuid.ToString)

End Sub

Private Sub DoStuff()

End Sub

End Class
//OUTPUT

10001000-1000-1000-1000-100010001000
10001000-1000-1000-1000-100010001000



这篇关于存储对值类型的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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