How to Dynamically Call Various Routines in VB.NET?

J

jaykchan

I would like to know how to call various routines dynamically using
VB.NET.

Let say I have a class called CMyBarcodeReader that needs to send a
barcode number that it has received from scanner to a routine called
HandleBarcodeNum() in one of these forms: frmOne, frmTwo, and frmThree.
Obviously, it should send the barcode number only to the currently
opened form, and the current form can be frmOne, frmTwo, or frmThree. I
can think of two possible ways to handle this:

One way is to pass the address of one of the HandleBarcodeNum() routine
to CMyBarcodeReader like what a C program may do. But I have no idea
how to do this in VB.NET. Can someone point me an example or some
useful keywords?

Another way that I can think of is to pass a flag to CMyBarcodeReader
class to indicate the currently opened form that it should send. And
then, when CMyBarcodeReader needs to send the barcode number to
HandleBarcodeNum() routine, it can check the flag and then call the
appropriate function. In other words, the code is like this:

CMyBarcodeReader:
..
..
Select(Me.m_sSendToFormName)
Case "frmOne"
Call frmOne.HandleBarcodeNum(sBarcodeNum)
Case "frmTwo"
Call frmTwo.HandleBarcodeNum(sBarcodeNum)
Case "frmThree"
Call frmThree.HandleBarcodeNum(sBarcodeNum)
End Select
..
..

The HandleBarcodeNum() routine will be defined like this in each form:

Public Shared Sub HandleBarcodeNum(ByVal sBarcodeNum As String)
....
End Sub

The problem is that I get syntax error when I refer to data elements of
the form. Seem like that routine "may" not get access to data elements
of that instance of form, and seem like a Public-Shared-Sub "may" only
get access to some kind of static data elements or class-level data
elements. How should I handle this situation? Can someone show me the
correct syntax or an example?

Is there a better way to do this?
Thanks in advance for any info.

Jay Chan
 
C

Cor Ligthert

Jay,

When you are thinking in operating assynchronosly between applications, you
would not have to think in my opinion in sending and receiving.

Do think in filling a queue and reading a queue. The reason is, that you can
difficult prevent that your reader is alreaydy reading while the other
process is still busy to process.

That is one of the purposes of vbNet remoting.

http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconnetremotingoverview.asp

I hope that it gives some idea's

Cor
 
J

jaykchan

The .NET remoting seems to be a bit more involved than I need. The
reason is that the functions are all within one application running in
the same handheld.

Anyway, I have fixed this problem by doing these:

- Create a frmBarcodeReader form class and let the main-form to
inherit from it. This simply adds the barcode scanning functionality to
the main form instead of using a separated object to handle barcode
scanning.

- The frmBarcodeReader raises an event whenever it gets a barcode
number, and the main-form handles the event, and forward the event to
the sub-forms.

- Because the main form creates all the sub-forms, it knows exactly
which form needs to receive the barcode number and it has access to the
sub-form instances. By doing this way, the main form can properly call
the public-shared-function in each sub-form to pass the barcode number
to it.

Jay Chan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top