1

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
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1628
  • 143
  • 1
  • 4
  • 2
    That article is for VB 6, not VB.NET. – vcsjones Dec 04 '12 at 16:37
  • Bear in mind that there are good reasons why vb.net (and c#) do not explicitly support pointers - primarily imo the simplicity aspect. Pointers are notoriously easy to screw up. You seem to be looking to introduce them and I'd suggest you walk this road very, very carefully. – PeteH Dec 04 '12 at 16:58
  • 1
    "converting C to VB Line for line" isn't recommended. Write something in .Net that DOES the same thing. As there is no direct translation – hometoast Dec 04 '12 at 17:00
  • Plus as @vcsjones says this article does not refer to vb.net. I saw some tricks with VB6 where you could use pointers to access API functions directly. I seem to remember one example being a multithreaded VB app (calling CreateThread directly from code). But these "tricks" were downright dirty, imo - you were certainly going way beyond the normal use of the language. From a maintainability perspective alon that's not good. – PeteH Dec 04 '12 at 17:04
  • @PeteH "Bear in mind that there are good reasons why vb.net (and c#) do not explicitly support pointers" Huh? C# has natively supported pointers from the beginning. The ability to seamlessly switch back and forth between managed and unmanaged code (and blend them) is one of the things I love most about C#. – reirab Apr 30 '20 at 06:15

2 Answers2

5

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
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 4
    `int *myNumber=3;` "works" in the sense that it assigns 3 to `myNumber`; the problem is that 0x00000003 is most likely *not* a valid address for an integer (or much of anything else). – John Bode Dec 04 '12 at 17:13
  • @JohnBode is a pedant after my own heart! – Steven Doggart Dec 04 '12 at 17:26
  • Does the ByRef make it a pointer or is every object just an address in vb.net? – user1628 Dec 04 '12 at 17:51
  • Every object is an address. Sort of. You see, like many interpreted languages, everything is an object that is created in an usually unreachable space (let's call it "the void" for a sci-fi feel). What you reach and manipulate with your code are references to the objects (sort of pointers). The interpreter usually counts how many references there are for a single object - when that count drops to zero, that object is ripe for garbage collection (thus freeing memory). – Carlos Vieira Oct 28 '13 at 13:24
  • @CarlosVieira How about Value Types? [WeakReference](http://msdn.microsoft.com/en-us/library/system.weakreference.aspx)? Boxing? Even in C, pointers aren't necessarily _addresses_, AFAIK. It's unspecified (hence implementation dependent) – sehe Oct 28 '13 at 14:09
  • sehe: What about them? How would talking about this stuff help the OP understand the basics of references in VB? It's easier (more didactic) at this stage to think of pointers and references as being sort-of-addresses, IMO. Also note the OP mentioned C, not C#. – Carlos Vieira Oct 29 '13 at 15:10
1

In .NET, there are two kinds of object types. There are reference types (Class) and there are value types (Structure). Instead of deciding, for each variable, whether or not that variable is a pointer, in .NET, all reference type objects are always treated like pointers and all value type objects are not. So, for instance:

'Create one object of each type
Dim builder As New StringBuilder()  'Reference type
Dim size As Integer  'Value type

'Make a "copy" of each
Dim copyBuilder As StringBuilder = builder
Dim copySize As Integer = size

'Change the value of the original object
builder.Append("Test")
size = 33

'Check if the "copy" changed
Console.WriteLine("{0} = {1}", builder.ToString(), copyBuilder.ToString())
Console.WriteLine("{0} <> {1}", size, copySize)

When you run that code, it will output this:

Test = Test
33 <> 0

So, as you can see, StringBuilder objects act like pointers whereas Integer objects do not. However, as Tony pointed out, even with value types, you can pass a value "by reference" to a method. When a method specifies that one of its parameters is ByRef, as opposed to ByVal, it means that the method may change the value of the variable internally and the change will affect the variable in the calling code.

Also, you may want to do some research into boxing and unboxing.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105