Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ANY in VB.NET

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use ANY in VB.NET. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

We will run into a lot of problems when using pointers. I don't know if you have encountered any problems in programming. I would like to share some of the problems I encountered before. Any is not a real type, it just tells the VB compiler to abandon checking on parameter types, so that, in theory, we can pass any type to API. Where can I use Any? Let's see what it says in the VB document. Now open MSDN (the native version of Visual Studio 6), turn to "Visual Basic documentation"-> "using Visual Basic"-> "tools Guide for parts"-> "access DLL and Windows API" section, and then take a look at the section "translating C language statements into Visual Basic statements". The documentation tells us that we use VB.NET ANY only if C is declared as LPVOID and NULL. In fact, if you are willing to take risks, you can use Any for all types. Of course, as I said, never use VB.NET ANY.

Why are you doing this? Then why do VB officials provide Any? Do you trust me or the VB official? Is there any reason not to use VB.NET ANY?

As mentioned earlier, VB officials do not encourage us to use pointers. Because one of the boasted advantages of VB is that there are no dangerous pointer operations, memory access is controlled by the VB runtime library. At this point, the JAVA language has the same boast. However, like JAVA, VB must overcome the problem caused by the absence of pointers in order to avoid using pointers for greater security. VB has made every effort to keep us away from pointers while having the security of strong type checking. But the operating system is written in C, which needs pointers everywhere, and some pointers are untyped, which is the terrible void* untyped pointers that C programmers often say. It has no types, so it can represent all types. For example, CopyMemory corresponds to the memcpy of C language, and its declaration is as follows:

Void * memcpy (void * dest, const void * src, size_t count)

Because the first two parameters of memcpy use void*, any type of parameter can be passed to him. A programmer using C should know that such a void* is not uncommon in C function libraries and how dangerous it is. No matter what type of variable pointer is passed to the void*,C compiler of memcpy above, it will not report an error or give any warning. Most of the time in VB, we use Any just to use void*, as in C, VB does not check the type of Any, and we can pass any type to the Any,VB compiler without error or warning. However, whether the program will make mistakes when running depends on whether you are careful when using it. Precisely because many errors in C are related to void*, C++ encourages us to use satic_cast to clearly point out this unsafe type of conversion, which is good for finding errors.

Having said so much about Any, I'm actually trying to tell all VB programmers that we have to be as careful when using Any as we are when using void*. There is no such thing as satic_cast in VB, but we can explicitly use the long type when passing pointers and use VarPtr to get pointers to parameters, which at least makes it clear that we are using dangerous pointers. For example, Program 2 becomes the following program after this treatment:

'Use a more secure CopyMemory and use pointers explicitly! Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long) Sub SwapStrPtr2 (sA As String, sB As String) Dim lTmp As Long Dim pTmp As Long, psA As Long, psB As Long pTmp = VarPtr (lTmp): psA = VarPtr (sA): psB = VarPtr (sB) CopyMemory pTmp, psA, 4 CopyMemory psA, psB, 4 CopyMemory psB, pTmp, 4 End Sub on how to use ANY in VB.NET Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report