Pointer and Variable

maxpic55

Newcomer
Joined
Dec 16, 2002
Messages
15
How do I do this in Visual Basic .NET?

I have this code:

dim a as integer = 1
dim pointer as object ="a"
msgbox pointer

I want that the result will be the value in variable A.

Thank you.
Maxpic55
 
="a" is saying that pointer is actually equal to the character a.

in your statement:
msgbox pointer

it is looking for a STRING where the word pointer is

you should be able to take out the
"dim pointer as object="a" and just say

msgbox(a)

I believe that will convert the value of a to a string, but you may need to do the conversion yourself. Haven't tested it...
 
If you wanted a string representation of whatever ponter holds you could always do
Visual Basic:
Dim pointer as Object = a
msgbox pointer.ToString()
'or as this is .Net 
MessageBox.Show(pointer.ToString()

If on the otherhand you want the object variable to now point to whatever memory location a was then that is a different matter.
It may help if you give an idea of why you need this functionality as there may be a simpler / better / more .Net way of getting the desired result.
 
Also, if the "pointer," better known as a reference in .Net because it does not behave like a C++ pointer in every aspect, points to something that you don't want to treat as a string (for instance, if you want to add two numbers) you can cast to the appropriate type.
Visual Basic:
Dim A As Integer = 3
Dim B As Integer = 5
 
Dim pA As Object = A 'Points to A (3)
Dim pB As Object = B 'Points to B(3)
 
Dim Total As Integer = CType(pA, Integer) + CType(pB, Integer)
MessageBox.Show(Total.ToString())
One of the differences between a pointer and a reference is that you can't do pointer math with a reference. Also, if you create a reference to a Value type (built in types like int, double, boolean, etc., or structures) you don't get a pointer to the original variable, but instead the value is "boxed" and you get a pointer to the boxed value. In addition, if you assign a new value to a boxed value type, instead of modifying the value you point to, a reference is made to a new boxed value, which means that changes made in one reference to a value will not be reflected in other previously equal references. It will probably be easier to understand what I'm saying (I'm not sure I understood that) in this code:
Visual Basic:
Dim i As Object = 5 
'i points to the boxed value 5
Dim j As Object = i
'j points to the [U]same[/U] boxed value
j = Cint(j) + 1
'instead of modifying the value that both i and j point to, j is assigned a newly
'boxed value of 6...
MessageBox.Show(i.ToString())
'...while i still points to the original 5.
In other words, don't expect .Net references to behave exactly like pointers.


Generally, boxed values can be avoided. Another method will usually be more suitable, depending on what you are doing. If you need functionality closer to a pointer, you might want to create a class to hold your integer. If it is just a value that you will need to access in alot of places, you might want to declare it statically (use the shared keyword) and make a static (again, shared in VB) property to access it.
 
Back
Top