在Visual Basic .NET C风格的指针 [英] C-style pointer in Visual Basic .NET
问题描述
我一直在调查在Visual Basic .NET C风格的指针有一段时间了。我所遇到的 http://support.microsoft.com/kb/199824?wa = wsignin1.0 ,但我不知道如果这是正确的事情还是如何应用它。我已经用C语言编写的程序使用一个简单的指针,我想它转换线线到Visual Basic与评论在必要。
这里是C:
INT的main()
{
为int *为mynumber = 3; //将*意味着它是一个指针 doubleIt(*为mynumber); //我们使用虚空中,*意味着它返回一个值没有解决
的printf(%D,mynumber的); //我们打印变量 返回0; //我们终止功能
}无效doubleIt(INT输入)
{
输入* = 2; //双输入
}
有一些问题,你的C code,其中之一是这样的:
为int *为mynumber = 3; //将*意味着它是一个指针
您不能赋值给这样一个指针,没有首先分配内存吧。
所以,你会做到以下几点:
为int * mynumber的malloc的=(的sizeof(INT));
*为mynumber = 3;
免费(mynumber的);
VB.NET没有指针的概念。一切(即每对象
)是一个参考,这是约接近三分球,将得到不使用互操作。如果你需要做的互操作还有的的IntPtr
键入可用于重新present指针类型。
您VB.NET程序可能看起来像这样:(原谅我,如果语法不完全正确的,它已经有一段时间)
副主
昏暗mynumber的作为整数= 3
doubleIt(mynumber的)
Console.WriteLine(mynumber的)
结束小组子doubleIt(VAL的ByRef作为整数)
VAL * = 2
结束小组
I have been investigating C-style pointers in Visual Basic .NET for a while now. I have come across http://support.microsoft.com/kb/199824?wa=wsignin1.0 but I have no idea if that is the right thing or how to apply it. I have written a simple pointer using program in c and I would like it converted line for line into Visual Basic with comments wherever necessary. Here's the C:
int main()
{
int *myNumber=3; //the * means it's a pointer
doubleIt(*myNumber); //we use the void, the * means it returns a value not address
printf("%d",myNumber); //we print the variable
return 0; //we terminate the function
}
void doubleIt(int input)
{
input*=2; //double the input
}
There's some issues with your C code, one of them being this:
int *myNumber=3; //the * means it's a pointer
You cannot assign a value to a pointer like that, without first allocating memory to it.
So you would do the following:
int* myNumber = malloc(sizeof(int));
*myNumber = 3;
free(myNumber);
VB.NET has no notion of pointers. Everything (ie every Object
) is a reference, and that's about as close to pointers it will get without using Interop. If you need to do interop there's the IntPtr
type which can be used to represent a pointer type.
Your VB.NET program might look something like this: (forgive me if the syntax isn't exactly correct, it's been a while)
Sub Main
Dim myNumber As Integer = 3
doubleIt(myNumber)
Console.WriteLine(myNumber)
End Sub
Sub doubleIt(ByRef val As Integer)
val *= 2
End Sub
这篇关于在Visual Basic .NET C风格的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!