Andrew Andrew - 4 months ago 9
Vb.net Question

VB Pass ActiveX Object to Function

Is it possible to pass an activex object to another function in the same class? I have tried a few variations but the first code sample is what I have tried most recently. The second set of code works currently but I would like to shorten it so that I don't have to write a seperate dunction for each instance of the object.

Here is my current code that I am trying:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay1")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay2")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay3")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay4")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay5")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay6")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay7")
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay8")
End Sub


Private Sub Login(disp As Object)
User = TextBox1.Text
Password = TextBox2.Text
disp.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
disp.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub


The code works fine if I write it like this.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
User = TextBox1.Text
Password = TextBox2.Text
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login1))
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login2))
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login3))
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login4))
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login5))
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login6))
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login7))
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login8))
End Sub


Private Sub Login1()
AxObjectXMFDisplay2.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
AxObjectXMFDisplay2.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub

Private Sub Login2()
AxObjectXMFDisplay2.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
AxObjectXMFDisplay2.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub

Private Sub Login3()
AxObjectXMFDisplay3.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
AxObjectXMFDisplay3.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub

Private Sub Login4()
AxObjectXMFDisplay4.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
AxObjectXMFDisplay4.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub

Private Sub Login5()
AxObjectXMFDisplay5.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
AxObjectXMFDisplay5.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub

Private Sub Login6()
AxObjectXMFDisplay6.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
AxObjectXMFDisplay6.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub

Private Sub Login7()
AxObjectXMFDisplay7.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
AxObjectXMFDisplay7.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub

Private Sub Login8()
AxObjectXMFDisplay8.RDE_SendKeys_Hsynch(User & "@T", True, 3, 1000, "", 0, 0)
AxObjectXMFDisplay8.RDE_SendKeys_Hsynch(Password & "@E", True, 3, 1000, "", 0, 0)
End Sub


Thanks

Answer

It is indeed possible, but in your attempt you are just not doing it quite right.

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), "AxObjectXMFDisplay1")

"AxObjectXMFDisplay1" is just a string containing the name of your ActiveX object. Placing quotes around something makes the compiler interpret it as a string.

To pass the actual ActiveX object you must remove the quotes so that you reference your actual variable/object:

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Login), AxObjectXMFDisplay1)

Do this for every line and you should be good to go.


What I explained above can be illustrated by this short example:

Dim MyNumber As Integer = 3 'Creates the variable 'MyNumber' and sets it to 3.

MessageBox.Show(MyNumber) 'Prints: 3
MessageBox.Show("MyNumber") 'Prints: MyNumber
Comments