What is the equivalent of a C pointer in VB.NET?

35,506

Solution 1

Nathan W has already suggested the IntPtr structure which can represent a pointer or handle, however, whilst this structure is part and parcel of the .NET framework, .NET really doesn't have pointers per-say, and certainly not like C pointers.

This is primarily because the .NET Framework is a "managed" platform and memory is managed, assigned, allocated and deallocated by the CLR without you, the developer, having to worry about it (i.e. no malloc commands!) It's mostly because of this memory management that you don't really have access to direct memory addresses.

The closest thing within the .NET Framework that can be thought of as a "pointer" (but really isn't one) is the delegate. You can think of a delegate as a "function pointer", however, it's not really a pointer in the strictest sense. Delegates add type-safety to calling functions, allowing code that "invokes" a delegate instance to ensure that it is calling the correct method with the correct parameters. This is unlike "traditional" pointers as they are not type-safe, and merely reference a memory address.

Delegates are everywhere in the .NET Framework, and whenever you use an event, or respond to an event, you're using delegates.

If you want to use C# rather than VB.NET, you can write code that is marked as "unsafe". This allows code within the unsafe block to run outside of the protection of the CLR. This, in turn, allows usage of "real" pointers, just like C, however, they still do have some limitations (such as what can be at the memory address that is pointed to).

Solution 2

C#, and I also believe VB.Net, will work on the concept of references. Essentially, it means when you say

A a = new A()

the 'a' is a reference, and not the actual object.

So if I go

B b = a

b is another reference to the same underlying object.

When you want to expose any internal objects, you can simply do so by exposing 'properties'. Make sure, that you do not provide setters for the properties, or that if you do, there is code to check if the value is legal.

ByRef is used when you want to pass the object as a parameter, and when you want the called method to be able to change the reference (as opposed to the object).

As mentioned above, if you post some code, it will be easier to explain.

Solution 3

Best way to do it is to just allocate everything manually:

You can move up OR down each Stack at free will without Pushing or Popping.

Dim Stack(4095) as Byte    'for 8bit   - 1 bytes each entry
Dim Stack(4095) as Integer 'for 16bit  - 2 bytes each entry
Dim Stack(4095) as Long    'for 32bit  - 4 bytes each entry
Dim Stack(4095) as Double  'for 64 bit - 8 bytes each entry

Dim i as integer           'Where i is your Stack Pointer(0 through 4095)
Dim int as integer         'Byte Integer Long or Double  (8, 16, 32, 64 bit)

for i = 0 to 4095

    int = i

    Stack(i) = int/256                         'For 8bit  Byte
    Stack(i) = int                             'For 16bit Integer
    Stack(i) = Microsoft.VisualBasic.MKL$(int) 'For 32bit Long
    Stack(i) = Microsoft.VisualBasic.MKD$(int) 'For 64bit Double

    MsgBox(Microsoft.VisualBasic.HEX$(Stack(i))) 'To See Bitwise Length Per Entry

next i
Share:
35,506
pistacchio
Author by

pistacchio

Updated on July 05, 2022

Comments

  • pistacchio
    pistacchio almost 2 years

    What is the most similar thing in VB.NET to a pointer, meaning like C pointers?

    I have a TreeView within a class. I need to expose some specific nodes (or leaves) that can be modified by external objects.