Returning ByRef? (a pointer to a pointer)

C

Chris Morse

Hi,

I'm trying to translate a small library written in C. Passing an
object/class parameter ByRef will result, in effect, in passing a
reference to the object reference, which is just like passing a
pointer to a pointer in C.

For example:

void build_it(Node** n)
{
// does it's thing...
}

In this case, I have in VB the following (including an example Class
definition, for clarity's sake):

Public Class Node
Public y As Double
Public left As Node
Public right As Node
End Class

Public Sub build_it(ByRef n As Node)
'Does it's thing...
End Sub

This works as expected and covers passing a reference to a reference
to a function.

My next task is RETURNING a reference to a reference.

For example, in C:

Node** work_it(Node* n)
{
// does it's thing
if(condition)
return &n.left;
else
return &n.right;
}

Converting this to VB does not seem possible. The idea here is that a
REFERENCE to the node.left or .right reference is returned so that it
can be modified by the calling function (or another function where
this return value is being passed ByRef)

I do not see any way of returning a reference to n.left or n.right in
VB - so that they can be modified.

Public Function work_it(ByVal n As Node) As ????
{
// does it's thing
If condition = True Then
Return n.left 'I want a reference to n.right, not just a copy
of the pointer n.right (as if returning ByRef and not ByVal)
Else
Return n.right ' <-- same as above
End If
}

I don't just want what n.left or n.right is pointing to, but I want a
reference to the actual n.left or n.right member variable.

Is there any facility in VB to do this? So far I have not found
one... It seems that I will be resigned to re-implementing the
algorithm since VB cannot express this directly.

Thanks!

// CHRIS
 
S

Scott M.

Chris Morse said:
Hi,

I'm trying to translate a small library written in C. Passing an
object/class parameter ByRef will result, in effect, in passing a
reference to the object reference, which is just like passing a
pointer to a pointer in C.

For example:

void build_it(Node** n)
{
// does it's thing...
}

In this case, I have in VB the following (including an example Class
definition, for clarity's sake):

Public Class Node
Public y As Double
Public left As Node
Public right As Node
End Class

Public Sub build_it(ByRef n As Node)
'Does it's thing...
End Sub

This works as expected and covers passing a reference to a reference
to a function.

My next task is RETURNING a reference to a reference.

For example, in C:

Node** work_it(Node* n)
{
// does it's thing
if(condition)
return &n.left;
else
return &n.right;
}

Converting this to VB does not seem possible. The idea here is that a
REFERENCE to the node.left or .right reference is returned so that it
can be modified by the calling function (or another function where
this return value is being passed ByRef)

I do not see any way of returning a reference to n.left or n.right in
VB - so that they can be modified.

Public Function work_it(ByVal n As Node) As ????
{
// does it's thing
If condition = True Then
Return n.left 'I want a reference to n.right, not just a copy
of the pointer n.right (as if returning ByRef and not ByVal)
Else
Return n.right ' <-- same as above
End If
}

I don't just want what n.left or n.right is pointing to, but I want a
reference to the actual n.left or n.right member variable.

Is there any facility in VB to do this? So far I have not found
one... It seems that I will be resigned to re-implementing the
algorithm since VB cannot express this directly.

Thanks!

// CHRIS

It seems to me that rather than trying to implement this in the function
that gets passed a Node (or whatever), you'd change the behavior of the
Node's Left/Right members, so that accessing them returns a reference to the
object.

-Scott
 
P

Phill W.

Chris said:
My next task is RETURNING a reference to a reference.

For example, in C:

Node** work_it(Node* n)
{
// does it's thing
if(condition)
return &n.left;
else
return &n.right;
}

Converting this to VB does not seem possible. The idea here is that a
REFERENCE to the node.left or .right reference is returned so that it
can be modified by the calling function (or another function where
this return value is being passed ByRef)

This may be the intention and (from memory) the kind of wacky,
performance-enhancing thing that C++ programmers do all the time, but it
makes for some pretty unreadable (and downright /dangerous/ to maintain)
code.
I would suggest that, if you want to change the left or right property
of a given Node, then you should do so /though/ the Node in question,
not through the use of some "excised" reference that happens to point
somewhere into its innards.
I don't just want what n.left or n.right is pointing to, but I want a
reference to the actual n.left or n.right member variable.

So you want the address (names won't do; they're all compiled out of the
generated exe/dll) of a member variable of a Class to be accessible
outside that Class. Sounds like a really Bad Idea to me. The code's
unclear - changing the reference modifies the behaviour of an object
without the object having any say about it - and it won't be
thread-safe, if that's something you worry about (changing it via the
Node object /could/ be)
Is there any facility in VB to do this?

I don't think so.

HTH,
Phill W.
 

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