r/visualbasic • • Aug 18 '20

[Noob] Does variable in Vb.net have concept of address?

Im doing my assignment on Vb net, I've been searching on the internet and books, but I can't seem to find anything related to the variable address. I found a question similar to mine in Stack Overflow. Someone replied

we dont. so we cant

in dotnet and java world, the concept of address of a variable is not the same as in C. the way address-of-a-variable is taught is old. the java/dot-net languages are way too advanced. of course the variable exists in memory but the address may not be what you think. so while working in these languages we do not usually consider the address aspect. that is considered only in context of C/C++.

Does this mean Vb net dont have the concept? If so, where does it store the variable?

5 Upvotes

5 comments sorted by

7

u/clarinetJWD Aug 18 '20

I believe you're talking about pointers. VB.net doesn't have full pointers, but you can get similar functionality by using "ByRef" if it's a parameter to a method.

For simple variables, primitive types (Integer, Char, Double, etc) are always by value. Objects like classes are always by reference (a pointer to the actual object)

For example:

Dim i As Integer = 1
Dim j As Integer = i
i = 2
' At this point, i=2 and j=1 because integers are assigned by value.

But take the following class:

Class MyClass
    Property MyProperty As Integer = 1
End Class

It works like this:

Dim myClass1 As New MyClass()
Dim myClass2 = myClass1

myClass1.MyProperty = 2

' Now myClass1.MyProperty=2 AND MyClass2.MyProperty=2 because myClass2 is just a reference to the same object.

Lastly, take the following method example:

Sub Main()
    Dim i As Integer = 1
    Dim j As Integer = 2

    MyMethod(i, j)
   ' Now i=3 and j=2 because I was passed byref and j was passed byval
End Sub

Sub MyMethod(ByRef i As Integer, j As Integer)
    i=3
    j=3
End Sub

Excuse any formatting issues... I'm on mobile.

3

u/CurryPudding Aug 18 '20

Thanks for the explanation. This helped a lot.

2

u/clarinetJWD Aug 18 '20

Awesome! Glad I could :)

1

u/DystarPlays Aug 18 '20

If you are talking about addresses in memory then yes, but no. All data is stored in memory, and all computers use addresses to know where in the memory that data is stored. Whether a language gives access to manipulating those addresses is the issue.

We talk of programming languages as being "High-Level", or "Low-Level" which refers to how much abstraction there is between what you write and how the computer interprets those instructions. These levels aren't a binary designation, but are comparative (C is lower level than C++), and scalar (C is High-Level but on the lower end of High-Level).

The lower level a language is, the more likely it is to give direct access to memory addresses. VB is relatively High-Level and one of the reasons it falls into this category is because it doesn't give access to memory addresses

1

u/CurryPudding Aug 18 '20

Thanks for answering my question. I understood the concept.