Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Why doesn't Visual Basic.NET have the addressof operator like C#? In C#, one can

int i = 123;
int* addr = &i;

But VB has no equivalent counter part. It seems like it should be important.

UPDATE Since there's some interest, Im copying my response to Strilanc below.

The case I ran into didnt necessitate pointers by any means, but I was trying to trouble shoot a unit test that was failing and there was some confusion over whether or not an object being used at one point in the stack was the same object as an object several methods away.

share|improve this question
3  
Should be on SO? –  FrustratedWithFormsDesigner Feb 18 '11 at 16:55
3  
VB (<= 6) or VB.NET? Because VB6 does allow this. Actually, you can sorta do it in VB.NET as well, but it's not built into the language. See: stackoverflow.com/questions/1956007/… for details. Unless you just want idle speculation on why MS designed the language this way, you should probably have asked this on SO to start with... (but don't - it's a duplicate). –  Shog9 Feb 18 '11 at 16:57
    
I thought programmers would be better than SO because I was under the impression that SO for specific troubleshooting problems but upon checking the faq again, it seems that this question may fall under there. I wasn't hoping for idle speculation, but rather that someone here would know the design decisions behind the language. –  Jeff Feb 18 '11 at 17:36
    
Oh, and VB.NET as the original post states :) –  Jeff Feb 18 '11 at 17:37
    
Oh, i see the confusion. I mention both. Thats what I get for being a wise guy. –  Jeff Feb 18 '11 at 18:13

3 Answers 3

up vote 6 down vote accepted

You can't use raw pointers in VB.Net, because it doesn't have unsafe blocks.

Even if VB did have unsafe blocks, like C#, it would better to not use them. Assemblies with unsafe code need special permissions in order to run, because they don't pass type checking.

I don't think this limitation is important. Pointers just aren't the way you do most things in .Net. Don't try to write VB code like it's a dialect of C. That being said, there are some cases (interop?) where it can be necessary and is certainly missed.

What case are you encountering where you think a pointer is the best option? Maybe you should be using a ByRef parameter or an array/offset/length triplet instead?

share|improve this answer
    
The case I ran into didnt necessitate pointers by any means, but I was trying to trouble shoot a unit test that was failing and there was some confusion over whether or not an object being used at one point in the stack was the same object as an object several methods away. –  Jeff Feb 18 '11 at 18:13

You can send a pointer to a certain variable in VB by specifiying the header of the function/sub in the following example:

Sub MySub(ByRef Var1 as String)
    Var1 = "Some Random Value"    'You can overwrite the value passed to the sub
End Sub

Note the keyword ByRef which means that this Sub accepts address of a variable. But assigning a new value to the variable does not need any special annotations.

but to make sure that you don't pass a copy of the Var1 to the sub, you need to call the sub like this:

MySub(AddressOf Var1)

instead of

MySub(Var1)

Although you are passing Var1 as input parameter, it will be overwritten the same ways as it would if you did something like:

Var1 = MySub()
share|improve this answer

The AddressOf operation serves to declare a delegate (event handler) from a method. Like this:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim handler As EventHandler = AddressOf ButtonClickHandler
End Sub

Public Sub ButtonClickHandler(ByVal sender As Object, ByVal e As EventArgs)
    ...
End Sub

without it VB.NET would try to call the method instead of just referencing its location. In C# this is not needed because ButtonClickHandler refers to the address of the method and ButtonClickHandler() calls the method. In VB.NET you can omit the parenthesis on a function and still be called. It is part of the crazyness of VB.NET trying to look like VB6 and feel like C# at the same time.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.