Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to reference an intptr to value (integer, for example), but I don't
find the solution

VB
Dim a As Integer = 100 (a = 100)
Dim b As IntPtr = a (b = 100)
a = 50 (a = 50)
b = 100 (b = 100)

I want to be 50, but I can't find the solution.

If I try that, in the first case b is really a, but not in the second case.

Is there any way to track the variables or do something like that?

Thanks in advance

What I have tried:

I have tried with events, wrappers, and even so I can't find the solution.

Could anyone help me?

Newly, thanks in advance
Posted
Updated 29-Apr-19 21:22pm
v2
Comments
phil.o 30-Apr-19 2:34am    
You cannot really mimic the C language with pointers in Vb.net or any other managed language. Here you should assign to b the address of variable a, not the value of variable a. But I don't know how to do it in vb.net. I know c# can understand unsafe blocks, but i'm not sure vb.net has an equivalent functionality. Managed code induces tge fact that you should not care about memory addresses, at all.

1 solution

I'm not sure i understand you well, but seems you want to track variable changes.

1. Tracking variables is messy for set of reason...
2. If you want to be informed about variable changes, use Class and call event in setter, for example:

VB.NET
Public Class myVar
    Private mValue As Integer
    Public Event VariableChanged(ByVal mvalue As Integer)

    Public Property Variable() As Integer
        Get
            Variable = mValue
        End Get
        Set(ByVal value As Integer)
            mValue = value
            RaiseEvent VariableChanged(mValue)
        End Set
    End Property
End Class


Usage:
VB.NET
Public Class Form1
    Private WithEvents test As New myVar
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        test.Variable = CInt(TextBox1.Text)
    End Sub
    Private Sub VariableChanged(ByVal NewValue As Integer) Handles test.VariableChanged
        MessageBox.Show(NewValue)
    End Sub
End Class


Above code comes from: vb.net - Detect variable change. [SOLVED] | DaniWeb[^]

If i'm wrong and you want to track variables while debugging, use breakpoints, see: Set a watch on variables - Visual Studio | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
phil.o 30-Apr-19 2:41am    
My 5 :)
Didn't you mean breakpoints instead of brakepoints?
Maciej Los 30-Apr-19 2:55am    
Oh yeah! The level of cafe in my body is too low... I need to fill in...
[EDIT]
Thanks! Changed ;)
EdwardCarnby 30-Apr-19 3:03am    
It worked, but I have to track manually the change. The thing is:
I'm trying to emulate a C-Like expression.
int a = 100;
int *p;
p = &a;
Something like this. I studied C many years ago, and I don't remember it very well,
but think that, in this way you obtained the adress of memory and when a changed, I obtained a value in p, with no matter the times a changed.
Is there any way?. Thanks, newly
Maciej Los 30-Apr-19 3:15am    
Even if i'm not sure what you mean by "track manually the change", the shortest answer is: No. Why? VB.NET is not a C.
Ralf Meier 30-Apr-19 3:44am    
What Maciej means here when he asks 'Why' :
Perhaps there is a possible Solution for you if we understand what you REALLY try to achieve ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900