Solved

How call and to pass parameters to ... TestRevised(sender As Object, e As EventArgs)  in vb.net

Posted on 2014-10-20
6
638 Views
Last Modified: 2014-10-21
There is a sub like:

Private Sub TestRevised(sender As Object, e As EventArgs) Handles FirstNameTextBox.TextChanged, LastNa...
.
.
.
End Sub

Question: How can I call this from another event?

Something like:  
Dim myTextBox As New TextBox("FirstName")  
myTextBox =CType( myTextBox , Object)

Then use  TestRevised(myTextBox ,???)

Question: How can I do something like this?

I know that I could move my code to a new sub without any parameters and call it from both of these subs. But I want to learn how I could do this with parameters as stated above.
0
Comment
Question by:Mike Eghtebas
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40393581
You would use:
Dim myTextBox As New TextBox("FirstName")
TestRevised(myTextBox, New EventArgs())

Open in new window


You don't have to cast myTextBox as an Object since myTextBox *is* an object.

-saige-
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40393600
Could you please teach me about what "New EventArgs()" is and what it transfers?
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40393611
It depends on the Event Arguments themselves.  Many event arguments carry information relevent to the event itself, e.g. 1.  Mouse related event arguments can carry information concerning the mouse; button state, pointer state, x and y axis coordinates.
2.  Grid related event arguments can carry information concerning the grid; the selected row or column.

This happens because ultimately all event arguments inherit from the base class EventArgs.

In the case of a button click event, no additional information is relevant because we know what is happening, a button has been clicked.  To determine which button it is, we pass the button as an object (because button's ultimately inherit from the base class object).  Since we know that we assigned this event to a button and we know which button it is we merely have to provide the action that happens when the button is pressed.

http://msdn.microsoft.com/en-us/library/system.eventargs(v=vs.110).aspx

-saige-
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 11

Expert Comment

by:louisfr
ID: 40393713
Instead of creating a New EventArgs(), I'd pass EventArgs.Empty to the event.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40394419
Here is an example where I created my own Event Arguments to signal that a thread has completed it's work:
Imports System.Collections.Generic
Imports System.Threading

Public Class Worker
#Region "ThreadWorderCompleted Memebers"
	Public Event ThreadWorkerCompleted As ThreadWorkerCompletedEventHandler

	Protected Overridable Sub OnThreadWorkerCompleted(ByVal sender As Object, ByVal e As ThreadWorkerCompletedEventArgs)
		RaiseEvent ThreadWorkerCompleted(sender, e)
	End Sub
#End Region

	Private shouldStop As Boolean = False
	Private m_id As Integer = -1
	Public Property ID() As Integer
		Get
			Return m_id
		End Get
		Private Set(ByVal value As Integer)
			If Not value.Equals(m_id) Then
				m_id = value
			End If
		End Set
	End Property

	Public Sub DoWork()
		Dim count As Integer = 0
		While Not shouldStop
			Console.WriteLine(String.Format("Worker Thread {0}: Working ({1} passes)...", ID, count + 1))
			' Simulating long running process.
			Thread.Sleep(200)
			count += 1
			If count = 5 Then
				shouldStop = True
			End If
		End While
		Console.WriteLine(String.Format("Worker Thread {0}: Terminating gracefully", ID))
		OnThreadWorkerCompleted(Me, New ThreadWorkerCompletedEventArgs(ID))
	End Sub

	Public Sub RequestStop()
		shouldStop = True
	End Sub

	Private Sub New()
	End Sub

	Public Sub New(ByVal ID As Integer)
		m_id = ID
	End Sub
End Class

Class Module1
	Shared reset As New AutoResetEvent(False)
	Shared workers As New List(Of Worker)()
	Shared creatingThreads As Boolean = False

	Shared Sub Main(ByVal args As String())
		creatingThreads = True
		For i As Integer = 0 To 4
			CreateAndStartThreads(i + 1)
		Next
		Console.WriteLine("Settings the reset event to block the main thread")
		reset.WaitOne()
		Console.WriteLine("Finished executing all threads.")
		Console.ReadLine()
	End Sub

	Public Shared Sub CreateAndStartThreads(ByVal ID As Integer)
		Dim worker As New Worker(ID)
		AddHandler worker.ThreadWorkerCompleted, AddressOf OnThreadWorkerCompleted
		Dim thread As New Thread(AddressOf worker.DoWork) With { _
		 .Priority = ThreadPriority.Normal, _
		 .IsBackground = True _
		}
		thread.Start()
		Console.WriteLine(String.Format("Adding worker with ID - {0}", ID))
		workers.Add(worker)

		If ID = 5 Then
			creatingThreads = False
		End If
	End Sub

	Public Shared Sub OnThreadWorkerCompleted(ByVal sender As Object, ByVal e As ThreadWorkerCompletedEventArgs)
		If TypeOf sender Is Worker Then
			Console.WriteLine(String.Format("Removing worker with ID - {0}", e.ID))
			workers.Remove(TryCast(sender, Worker))
			If Not creatingThreads AndAlso workers.Count = 0 Then
				reset.[Set]()
			End If
		End If
	End Sub
End Class

Public Delegate Sub ThreadWorkerCompletedEventHandler(ByVal sender As Object, ByVal e As ThreadWorkerCompletedEventArgs)

Public Class ThreadWorkerCompletedEventArgs
	Inherits EventArgs
	' Ideally you really want to use items that can identify the thread.
	Public Property ID() As Integer
		Get
			Return m_ID
		End Get
		Private Set(ByVal value As Integer)
			m_ID = value
		End Set
	End Property
	Private m_ID As Integer

	Private Sub New()
		ID = 0
	End Sub

	Public Sub New(ByVal ID As Integer)
		Me.ID = ID
	End Sub
End Class

Open in new window

Notice how the ThreadWorkerCompletedArgs class inherits from EventArgs -
Public Class ThreadWorkerCompletedEventArgs
	Inherits EventArgs
...
End Class

Open in new window


And then in the OnThreadWorkerCompleted event method, I use the ID of the Worker that I passed in in order to display which worker is affected and then cast the sender as a Worker in order to remove it from the list of workers.

-saige-
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40394680
I appreciate for the sample. I have to read it couple of times to get comfortable with it and be able to use it later. Later, I may have new followup questions.

Thanks,

Mike
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question