Is it possible to use pointers in VB.NET?

3

Is it possible to use pointers in VB.NET? If so, how do they work?

An example in C++ :

CSR = ConfigurarInterfaceDeRede(randInt(0,999999),CodAtivacaoSAT,(char*)xml);

int  a;
int *p;
p = &a;

How would you translate this to VB.NET ?

    
asked by anonymous 04.12.2014 / 13:12

1 answer

5

No, you can not directly.

Alternative

The maximum you can use is the IntPtr available in .NET. It is a more limited form, although it allows a reference to a point of memory being created in VB.NET. Internally it is represented as void * , but basically it is only Integer for VB.NET.

You can not dereference it (it even gives the class InteropServices.Marshal ") or do direct arithmetic with the pointer (indirectly gives). In some cases the use of GCHandle can also be useful

For what pointers?

The most important thing is that you generally do not need language pointers. The idea of VB.Net is another, is to facilitate your life and not bring complications. The way the language was built it is possible to do everything you would normally do with pointers without using them.

Most of the required constructs can be obtained from other, higher-level, more abstracted forms.

Of course this may not be as performative as possible, but it solves the problem and this is the philosophy of VB.NET. If you need performance yourself, do only what is really needed for performance in another language and integrate with your VB.NET program (this integration is simple in language that runs on top of the CLR). If you know how to use pointers, you'll know how to use another language.

Pointers are opaque

Understand that there are pointers on the platform, just not visible to the language. When you use a class, you are certainly using pointers, but you do not see them. When using delegates or lambdas , you are using pointer, even because they are classes as well. You just do not see them. Instead of using char * you use the String class (they are not completely compatible but serve the same purpose, an array of byte s is closer than it is a char * ).

When you need to pass an argument that is not a reference (classes for example) by reference, you use ByRef in the parameter, so you do not need a pointer.

Sub Main()
  Dim y As Integer
  Test(y)
End Sub

Sub Test(ByRef x As Integer)
  x = 42
End Sub

I placed GitHub for future reference .

Some classes have their own methods that help you do certain operations that would normally require a pointer.

When the pointer really is needed.

Whether to give power, whether to give flexibility or to give speed, there are cases where the pointer is needed.

In these cases you should use other languages supported in the CLR. .NET does this in its internal implementation. Note that it was developed with C # because this is a more powerful language. it is possible to use pointers in a limited way through blocks unsafe .

You can also use pointers in C ++ in a fairly comprehensive way but still managed by the garbage collector of .NET. Usually this is done more in cases of interoperability and in extreme cases of need for speed.

In fact it is possible to use languages outside the CLR. The .Net implementation does this all the time. It calls the Windows APIs written in C in an unmanaged way. When that full control, including memory, this may be the solution.

For interoperability you can solve with the classes passed above and some extra attributes. You will have to learn all aspects to marshalize access.

    
04.12.2014 / 13:35