Is it possible to use pointers in VB.NET?

Asked

Viewed 523 times

4

Is it possible to use VB.NET pointers? 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 I translate this to VB.NET?

  • 2

    Hello. And possible (there is something similar to pointers in the form of the Inptr structure). The most important question, why? What do you need pointers for? From some context to better answer.

1 answer

7


No, directly you can’t.

Alternative

The most you can use is IntPtr available in .NET. It is a more limited form, although it allows a reference for a memory point to be created in VB.NET. Internally he is represented as a void *, but deep down it’s just one Integer for VB.NET.

It is not possible to deregister it (even gives with the class InteropServices.Marshal) or do direct arithmetic with the pointer (indirectly gives). In some cases the use of GCHandle may also be useful

What pointers?

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

Most of the necessary constructions can be obtained from other forms of higher level, with more abstraction.

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 really need performance, just do what is really necessary 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 will know how to use another language.

Hands are opaque

Understand that there are pointers on the platform, only they are not visible to the language. When you use a class, you are certainly using pointers, but you don’t see them. When you use delegates or Amble, is using pointer, because they are classes too. You just don’t see them. Instead of using a char * you use the class String (they are not fully compatible but serve the same purpose, a array of byteis closer than is a char *).

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

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

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

I put in the Github for future reference.

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

When the pointer is really needed.

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

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

You can also use C++ pointers quite comprehensively but still managed by Garbage Collector of .NET. This is usually done more in cases of interoperability and in extreme cases of need for speed.

In fact it is possible to use languages outside of CLR. The . Net implementation does this all the time. It calls Windows Apis written in C unmanaged form. When total control, including memory control, this can 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.

  • I understand, I’m still studying this, because I need to talk to an equipment that in case the functions and variables of example are in C++ and use these methods of Return Pointer, in some functions. So I will try to do without using pointers. Thanks!!!

  • Thank you very much, Bigown! You are helping me a lot with these examples and this clear explanation. Thank you very much.

  • If you have any questions to do VB.Net interoperability with C or C++, you can ask another specific question about this. I’ve never done anything with it but it’s possible that other people have done it.

  • All right, I’ll do it as soon as I come along. But even though I didn’t do anything you helped me a lot, rsrs Thanks for now, strong hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.