Solved

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

Posted on 2014-10-20
6
739 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 34

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 34

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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 11

Expert Comment

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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