Storing a reference to a value type

J

Jonas Pohlandt

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.MyProperty) '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.
 
O

One Handed Man \( OHM - Terry Burns \)

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
 
J

Jonas Pohlandt

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.
 
O

One Handed Man \( OHM - Terry Burns \)

But all an identifier is 'IS' a reference to an object. The only difference
between a value type and a reference type with VB.NET, is that the reference
type is stored on the heap, whereas a value type is stored on the stack.

Using an identifier in VB.NET is the same as d-referencing an object pointer
in C++

Dim a,b,c as Int

a= 5
b=a
c=b

a,b,c all are references to value types stored on the stack.


Does this help.?


--

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

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

One Handed Man \( OHM - Terry Burns \)

If you need to set a private member variable at any time you can set this
using a property,.

/Button

Dim mc As New Work
mc.RefUid = New Guid("00010001000100010001000100010001")
Debug.WriteLine(mc.RefUid.ToString())


//Class

Public Class Work

Private m_RefUid As New Guid

Public Sub New()

End Sub

Public Sub DoStuff(ByRef uid As Guid)
uid = RefUid
Debug.WriteLine(RefUid.ToString)
End Sub

Public Property RefUid() As Guid
Get
Return m_RefUid
End Get
Set(ByVal Value As Guid)
m_RefUid = Value
End Set
End Property


End Class


--

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

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

Jonas Pohlandt

Hey Terry,

thanks for staying with me. ;-)

I think we're still talking about different things. Lets try this approach:
How would you implement a windows form where you pass a reference to a Guid
in the construktor and then show it modal. Upon pressing a button (and not
before that), the variable we passed in initially is assigned a specific
value, like getting the uid out of a textbox where we typed it in earlier.

Like this


dim uid as Guid
dim frm as new myFormClass(uid)

dim dr as DialogResult = frm.ShowModal()

system.console.out.write(uid) 'uid holds now the desired value.


How would you do this?

Thanks, Jonas
 
C

Chris Dunaway

How would you implement a windows form where you pass a reference to a Guid
in the construktor and then show it modal. Upon pressing a button (and not
before that), the variable we passed in initially is assigned a specific
value, like getting the uid out of a textbox where we typed it in earlier.

One way would be to make a public readonly property in the dialog form and
then get that value when you close the form:

'** In the form class **
Private m_myGuid As Guid

Public ReadOnly Property MyGuid() As Guid
Get
Return m_myGuid
End Get
End Property

Private Sub Button1_Click(...) Handles Button1.Click
m_myGuid = Guid.NewGuid
End Sub


'** In the calling code

Dim uid as Guid
Dim frm As New myFormClass()

If frm.ShowDialog() = DialogResult.OK Then
uid = frm.MyGuid
End If

frm.Dispose

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
O

One Handed Man \( OHM - Terry Burns \)

I think I understand you now, but your thinking is still frimly wedged in
C++ Pointers.

You cannot do this in VB.NET.

You would need to pass this variable to the function being called so that it
can be set ( as can the private member of this class ). But this does not
sound like good OOP to me, it means you are working with a variable outside
the class.






--

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

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

Jonas Pohlandt

'** In the calling code

Dim uid as Guid
Dim frm As New myFormClass()

If frm.ShowDialog() = DialogResult.OK Then
uid = frm.MyGuid
End If

frm.Dispose

That's what I was looking for. I just assumed frm would have been disposed
allready by the time the line
uid = frm.MyGuid is reached. Thanks!
 
J

Jonas Pohlandt

I think I understand you now, but your thinking is still frimly wedged in
C++ Pointers.

You cannot do this in VB.NET.

You would need to pass this variable to the function being called so that it
can be set ( as can the private member of this class ). But this does not
sound like good OOP to me, it means you are working with a variable outside
the class.

You are right, the design is flawed. I ended up subclassing DialogResult and
having the subclass include the Guid.
Thanks for taking time to think about this.
 
O

One Handed Man \( OHM - Terry Burns \)

Your welcome, good luck in your future .NET experience.

--

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top