C-style pointer in Visual Basic .NET

后端 未结 2 2028
一个人的身影
一个人的身影 2021-01-23 12:19

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 th

2条回答
  •  情话喂你
    2021-01-23 12:39

    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
    

提交回复
热议问题