Vb.net 指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/759474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:10:45  来源:igfitidea点击:

Vb.net Pointers

vb.netpointers

提问by pistacchio

What is the most similar thing in vb.net to a pointer, meaning like C poinetrs?

vb.net 中与指针最相似的东西是什么,意思是像 C poinetrs?

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

我在一个类中有一个 TreeView。我需要公开一些可以被外部对象修改的特定节点(或叶子)。

回答by Tanmay

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

C#,我也相信 VB.Net,会处理引用的概念。本质上,这意味着当你说

A a = new A()

A a = 新 A()

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

'a' 是一个引用,而不是实际的对象。

So if I go

所以如果我去

B b = a

B b = a

b is another reference to the same underlying object.

b 是对同一底层对象的另一个引用。

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).

当您希望将对象作为参数传递,并且希望被调用的方法能够更改引用(而不是对象)时,将使用 ByRef。

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

如上所述,如果您发布一些代码,会更容易解释。

回答by CraigTP

Nathan W has already suggested the IntPtrstructure 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.

Nathan W 已经提出了可以表示指针或句柄的IntPtr结构,然而,虽然这个结构是 .NET 框架的一部分,但 .NET 确实没有指针,当然不像 C 指针。

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.

这主要是因为 .NET Framework 是一个“托管”平台,内存由 CLR 管理、分配、分配和解除分配,而开发人员不必担心(即没有 malloc 命令!)主要是因为这个内存管理,您实际上无权访问直接内存地址。

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 delegateas a "function pointer", however, it's not really a pointer in the strictest sense. Delegatesadd 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.

.NET Framework 中最接近可以被认为是“指针”(但实际上不是)的是delegate。您可以将委托视为“函数指针”,但是,在最严格的意义上,它并不是真正的指针。 委托为调用函数添加了类型安全性,允许代码“调用”委托实例以确保它使用正确的参数调用正确的方法。这与“传统”指针不同,因为它们不是类型安全的,仅引用内存地址。

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

委托在 .NET Framework 中无处不在,无论何时使用event或响应事件,都在使用委托。

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).

如果您想使用 C# 而不是 VB.NET,您可以编写标记为“不安全”的代码。这允许不安全块中的代码在 CLR 的保护之外运行。反过来,这允许使用“真实”指针,就像 C 一样,但是,它们仍然有一些限制(例如可以在指向的内存地址处使用什么)。

回答by Jason

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

回答by Nathan W

If you are using VB the only thing that is really close to a pointer is a IntPtr. If you have access to C# you can use unsafe C# code to do pointer work.

如果您使用 VB,唯一真正接近指针的东西是IntPtr。如果您有权访问 C#,则可以使用不安全的 C# 代码来执行指针工作。

回答by dommer

If you're looking to pass something back from a subroutine you can pass it by reference - as in "ByRef myParamter as Object".

如果您希望从子例程传递回某些内容,您可以通过引用传递它 - 如“ByRef myParamter as Object”。

Best answer depends, to some degree, on what you're trying to do.

最佳答案在某种程度上取决于您要做什么。