Reference Passing...address of

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay,

Again a moment for the uninspired lack of pointer control, but here's my issue:

I have a reference passed parameter that I basically determine if the original variable was null (nothing) I assign it a value. However, sometimes, that parameter is a Class Field and other times it is a local stack variable. How do I determine if the ByRef Param is the Class Field or if it isn't?

example:
VB.NET:
public class X

private _myField as Integer

public sub MyProc()
  dim i as integer
  if  DoSomthing(i) then
    '<code>
  end if
end sub

public function DoSomething(byref I as integer) as boolean
  if AddressOf(I) = AddressOf(_myField) then
    'code
  end if
  I = MyComputation
end function
end class

Note, I am aware that the AddressOf() is not right. I've Tried Equals, that doesn't work because on objects it sees that Nothing = Nothing and returns true. ReferenceEquals() does the same. In Delphi (or C++) I in this instance is only retaining an Address of the original memory location for the variable, thus I could very easily compare addresses of my class field and the parameter being passed. So far I have not found a comparable method of comparing pointer addresses in VB.Net.
<edit>
Additionally, I would often like to create Optional ByRef parameters, basically, like the old VB6, if that's possible. In VB6 it used to have the ability to test parameters with IsMissing() but so far I have not found it in VB.Net.

Thanks
 
I'm a little confused. Your current parameter is of an Integer type so therefore it will live on the stack you can't pass a reference type to that function so how can it 'Sometimes be a Class Field and sometime be a local variable. A class is a reference type it inherits from the base class MarshallByReference so therefore it will always live on the managed heap. There's no need to know the memory address of a local variable on the stack, it will be pushed on for evaluation then popped off ( As a point of fact the first four local (Value type) variables I believe are loaded straight into the processor's registers. When you pass a value type by reference you do have a pointer to it's location on the stack you just don't know the memory address as VB.Net automatically dereferences all types passed by reference.
You can compare the memory addresses of two reference types like this...

VB.NET:
Dim gh As GCHandle = GCHandle.Alloc (SomeReferenceType, GCHandleType.Pinned)
Dim AddrOfMyString As IntPtr = gh.AddrOfPinnedObject()

We pin the object so it won't get moved by the GarbageCollector and then assign an IntPtr (Vb.Net's version of a pointer) to it's location on the heap. Then do the same for your other reference type and comapre the Intptr's however I believe ReferenceEquals does pretty much that.

Have I missed the point here?
 
Sort of and No.

The Point is that there are no Pointers...and I guess i've programmed for too long with the knowledge that everything is a pointer. Even those stack variables are pointers, and only in the smallest of smallest values that are easily shifted into registers for such purposes as you've mentioned with parameter passing, but then they are shifted back into pointers for data storage. Even if those pointers are created through a push/pop on the stack, it's still a memory address.

That being said, I probably should've used objects instead of Integers. There is a concept that I think everyone completely fell asleep during in English class, called Theme. I give the Theme of my idea, the details are meaningless and superfluous to the actual question, but more often than not people will read the question then focus on the details that are inconsequential. My question was about accessing the address of a variable in vb, not about integers versus classes.

Anway, as with what you described on the pinning and intptr, that is way too complicated a task. I think this will get voted off the island along with the fact you cant typecast a Char to Byte. Now that is just stupid. But that's vb.net.

*sigh*, anyway, i solved the issue with an optional Boolean flag.

basically, I'm creating connections to a Database, but sometimes I'm making a temporary connection, and sometimes I'm making the classes' global connection. If I'm creating the global connection (class field) then I need to do some other things to make sure I close it down. If i'm creating a temporary connection (for say the SqlDataReader for a SqlBulkCopy operation) then that connection is automatically closed on its own through the reader. I needed it for other more indepth reasons, but suffice it to say ReferenceEquals did NOT work, but the Boolean Flag Parameter does just fine.
Thanks Anyway
 
Well I think I kind of understand what you are saying though it's still very unclear. A class field is always passed with with the MSIL instruction 'ldelema' the important part here is the a on the end meaning 'address of' So a class field (Value type) is not copied on the stack at all, you always use it's memory address.

The Point is that there are no Pointers...and I guess i've programmed for too long with the knowledge that everything is a pointer.

Well yes, everything IS a pointer even in VB.Net, thats how memory works.

I would often like to create Optional ByRef parameters

I can't think of a scenario where i've ever used those or would I want to. I wite a lot in C/C++ and I would never leave my program open to something like that.
 
Back
Top