Re: Thread Execute Plug-in
If you're passing an Object(), just put the subname in that array too so you only have one parameter to pass.
Code:
Public Sub ThreadExecute(ByVal ParamArray Parameters() As Object)
TExec(Subname, Parameters)
End Sub
Justin
Re: Thread Execute Plug-in
That is not going to work, because I need to pass an address to a sub.
For the iinformation, the ThreadExecute sub is used the following way:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ThreadExecute(AddressOf looptest, 20000)
End Sub
Private Sub looptest(ByVal StartVal As Integer)
Dim i As Integer = StartVal
Do
i += 1
Debug.WriteLine("Current value is: " & i)
Loop
End Sub
If I reference to a different sub with 2 parameters, it uses the delegate:
Public Delegate Sub DSparam2(ByVal Param1 As Object, ByVal Param2 As Object)
And with 3 parameters:
Public Delegate Sub DSparam3(ByVal Param1 As Object, ByVal Param2 As Object, ByVal Param3 As Object)
I can't use an object for byval type, because it then gives the error:
"'AddressOf' expression cannot be converted to 'Object' because 'Object' is not a delegate type."
So, I can't use an object and have to use a delegate type of the list.
(Question is getting more and more trickier...:sick:)
Re: Thread Execute Plug-in
I did something like the following, and it worked just fine:
vb Code:
Dim obj As Object()
obj = New Object() {New Hello(AddressOf updateTB), "text", 1}
Dim d As Hello = obj(0)
Me.Invoke(d, New Object() {"it worked!"})
Dim s As String = CStr(obj(1))
MessageBox.Show(s)
You can store the delegate object an object array after it has it's addressof info set. then invoke that method on the appropriate form.
Justin
Re: Thread Execute Plug-in
Uh lol that is not the intention of my coding. xD
No "invoke" may be used, because then it is not running on a thread but on a form.
The big question is:
- How can I specify a list of types to use in a byval sub, without using the object type (can't use it as a delegate) and without overloading?
So for example:
Code:
Private Sub Test(ByVal A As (integer or string))
If A.GetType.Name = "string" Then
'process string: A
ElseIf A.GetType.Name = "integer" Then
'process integer: A
End If
End Sub
I want to use this sub like this: (with different parameters)
Code:
Test(AddressOf TheSub, Parameters)
The sub "TheSub" can have different parameter counts, so therfore I need to use multiple different delegates with different parameter counts.
No codes like this:
//cant set an object to addressof because it isnt a delegate type
Code:
Private Sub Test(ByVal A As Object)
If A.GetType.Name = "string" Then
'process string: A
ElseIf A.GetType.Name = "integer" Then
'process integer: A
End If
End Sub
//this is overloading and will result in, eventually, 20 - 30 subs which is just too much
Code:
Private Sub Test(ByVal A As Integer)
'process integer: A
End Sub
Private Sub Test(ByVal A As String)
'process string: A
End Sub
What do I have to do, without overloading as I do now, to use multiple types?
Re: Thread Execute Plug-in
Quote:
What do I have to do, without overloading as I do now, to use multiple types?
I don't think you can... Even if you could, the type passed (which would encompass all types) would still have to be cast to the specfic type to be used. That's what Object is for, but in your case won't work, so overloading is your best bet.
Justin
Re: Thread Execute Plug-in
To be able to pass a single parameters with mulitple possible type values, you either pass some sort of generic(object) collection(array/arraylist) or you create a class with those desired properties with corresponding types and set/get them as needed.
Justin
Re: Thread Execute Plug-in
Both good replies, thank you :D
The overloading works, but I would indeed need an overlapping type.
Then that changes the question a little, how can I pass a sub name without using a delegate?
Re: Thread Execute Plug-in
If you just want to pass the name of the sub routine, then do that. Then in whatever function you pass it to, you could compare the sub name provided and then execute that given sub routine.
For Example:
Code:
Public Sub ExecuteSubRoutineByName(SR as String)
If string.equals(SR,"LoadStrings",StringComparison.CurrentCultureIgnoreCase) then
LoadStrings()
End If
End Sub
That's the only way I could think of doing it by name. Else you would have to use the Delegate (addressof function). If you delcare a delegate in your class that you're passing the addressof to, and then set it equal to that address of passed to your function, it would work just fine.
Code:
Public Class DelTest
Public Delegate Sub DoSomething(str as string)
Public Delegate Sub DoSomethingInt(i as integer)
Public Sub New(del as Object, param as string)
try
Dim d as DoSomething = del
d.Invoke(param)
catch ex as exception
' wrong new sub to call with that delegate
end try
End Sub
End Class
But you can still see that you'll have to try and match up the delegate to the certain signature of the function that you pass to the constructor.
Justin
Re: Thread Execute Plug-in
Thanks that helped a little in my understanding of delegates :D
Also, I read some page explaining the use of address pointers pointing towards memory to get the sub name. One thing is clear, subs and functions can't be looked up using code. They are just "there" and you can't loop through them like an array.
But, my class still is freaking awesome! I can now easily "thread" a sub without the nasty repetitive lines.
For example; this is a delegated sub: before and after:
Before:
Code:
Delegate Sub myProgressbarAdapter(ByVal percent As Integer)
Private Sub ProgressbarAdapter(ByVal percent As Integer)
If Me.InvokeRequired Then
Dim d As New myProgressbarAdapter(AddressOf ProgressbarAdapter)
Me.Invoke(d, percent)
Else
Progressbar1.Value = percent
End If
End Sub
After:
Code:
Private Sub ProgressbarAdapter(ByVal percent As Integer)
If Me.InvokeRequired Then
DelegateExecute(Me, AddressOf ProgressbarAdapter, percent)
Else
Progressbar1.Value = percent
End If
End Sub
It really enhanced the coding :thumb:
The following problems still seem unexplained to me:
- Why can't you have two optional byval parameters?
- Why can't you give a delegate a paramarray?