Is it possible for a 3rd party to hold on to a variable reference in vb.net?
For example when i pass a variable to a function byref i can use it to directly modify the other class's variable but only while in scope of that function, i want to store that reference to do just that but later. In c++ i would just pass the variable pointer and store that in a variable for later use but how would you do that in vb?
Re: Is it possible for a 3rd party to hold on to a variable reference in vb.net?
All class variables are references, even when you pass them by value:-
vbnet Code:
'
Public Class Form1
Private ca As New ClassA
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ChangeValue(10, ca)
MsgBox(ca.Value)
End Sub
Private Sub ChangeValue(ByVal value As Integer, ByVal clsA As ClassA)
clsA.Value = value
End Sub
End Class
Public Class ClassA
Private _value As Integer = 0
Public Property Value() As Integer
Get
Return _value
End Get
Set(ByVal value As Integer)
_value = value
End Set
End Property
End Class
Although ca is passed by value to ChangeValue, the message box would display the value set inside the sub because all that is passed is a reference to that class. Classes create reference types.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Re: Is it possible for a 3rd party to hold on to a variable reference in vb.net?
thanks but that totally doesn't help me at all. heres what im trying to do
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Threading.Thread(Sub() Tweener(Button1.Width, 100, 400))
t.Start()
End Sub
Public Sub Tweener(ByRef target As Object, ByVal Amount As Integer, ByVal tPeriod As Integer)
Dim stopat As Integer = Environment.TickCount + tPeriod
Dim tFrame = 40
Dim chunk As Integer = Amount / (tPeriod / tFrame)
While Environment.TickCount < stopat
target += chunk
Application.DoEvents()
Threading.Thread.Sleep(tFrame)
End While
End Sub
The idea is that i could throw whatever variable, amount and time period to the tweener and it would deal with the animation instead of me having to explicitly set it up for said variable. Alas the code doesn't work since vb doesn't allow cross thread form manipulation.
Re: Is it possible for a 3rd party to hold on to a variable reference in vb.net?
Your problem has nothing to do with references or pointers or any of that. You simply cannot alter UI elements from any thread other than the UI thread. Attached below is an example of how to properly alter UI elements from another thread. You can read from this post to understand why you can't alter controls directly from foreign threads.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Re: Is it possible for a 3rd party to hold on to a variable reference in vb.net?
i dont want to alter it from another thread, what i want is to delegate the management to an universal construct of some kind. If i could store the reference of a variable i could do it via a time for example from within the same thread, it doesn't matter which way i get to it as long as i do.
Re: Is it possible for a 3rd party to hold on to a variable reference in vb.net?
You cannot pass a reference to the Width property, as you'd need to pass the backing field value ByRef rather than the property, and you don't have access to that. What you could do is pass the button itself, although then you'd be tied to altering the Width property on anything you passed. What I might be tempted to do is instead pass a Func and an Action. Assuming that you solve the cross threading issue separately (i.e. so I'm not completely rewriting your sample) you could look at something like this:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Threading.Thread(Sub() Tweener(Function() (Button1.Width), Function(newWidth As Integer) (Button1.Width = newWidth), 100, 400))
t.Start()
End Sub
Public Sub Tweener(ByVal getTarget As Func(Of Integer), ByVal setTarget As Action(Of Integer), ByVal Amount As Integer, ByVal tPeriod As Integer)
Dim stopat As Integer = Environment.TickCount + tPeriod
Dim tFrame = 40
Dim chunk As Integer = Amount / (tPeriod / tFrame)
While Environment.TickCount < stopat
setTarget(getTarget() + chunk)
Application.DoEvents()
Threading.Thread.Sleep(tFrame)
End While
End Sub
Re: Is it possible for a 3rd party to hold on to a variable reference in vb.net?
Another option is to model the concept of an animatable value with its own type:
vbnet Code:
Public Interface IAnimatable(Of T)
Property AnimationValue As T
End Interface
Which you can provide a concrete implementation for animating the width of a control - which can as part of its implementation deal with the issues of cross threading:
vbnet Code:
Public Class ControlWidthAnimator
Implements IAnimatable(Of Integer)
Private _control As Control
Private _width As Integer
Public Sub New(control As Control)
_control = control
_width = _control.Width
End Sub
Public Property AnimationValue As Integer Implements IAnimatable(Of Integer).AnimationValue
Allowing your Tweener code to deal with the abstract concept of an animatable value:
vbnet Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Threading.Thread(Sub() Tweener(New ControlWidthAnimator(Button1), 100, 400))
t.Start()
End Sub
Public Sub Tweener(ByVal animatable As IAnimatable(Of Integer), ByVal Amount As Integer, ByVal tPeriod As Integer)
Dim stopat As Integer = Environment.TickCount + tPeriod
Dim tFrame = 40
Dim chunk As Integer = Amount / (tPeriod / tFrame)
While Environment.TickCount < stopat
animatable.AnimationValue += chunk
Application.DoEvents()
Threading.Thread.Sleep(tFrame)
End While
End Sub
(I'd still deal with the animation with some kind of timer (be that a Windows.Forms.Timer on the UI thread or a System.Threading.Timer that returns on a worker thread) rather than sleeping in a worker thread, but now that's an orthogonal aspect of the code that can be altered independently of how the control itself gets altered.)