delegates???

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am reading a CSV file which could be like this -

Name1 functionName
PK isNumeric
AB isAlpha
CD isDate

I read this CSV file and have all the records in a dataset.
When I come to column 2 which is the function name, I have to invoke the
method isNumeric or isalpha or isDate or any such method.
now, the methods would onlt take a single parameter and return a boolean.
Since I don't know the name of the function, I thot delegates is the best
way , kind of function pointers. I am not sure of the syntax

step 1 : declare delegate
Step 2 : create delegate variable
step 3 : ??? how to assign the address of a function to the delegate . The
function name is in a variable????
step 4: invoke the method.

Can someone fill up on step 3???
Is this the right way to do???

Thanks
PK
 
Hi P K,

Chances are, a delegate is not going to be the solution to this problem. In
this case, the problem is knowing what method to call. A delegate is similar
to a function pointer, in which you can treat a function like a parameter,
or like a variable. But it doesn't solve the problem of knowing what
function to call.

There are several possible solutions to this, but the least expensive one is
probably going to be to use a switch statement. Compare the value in the
second column, and call the appropriate function based upon that. Yes, you
could use a single call to a delegate function, but you would have to assign
that function to the delegate in any case. If you do use a delegate, make
sure that the methods all have the same signature, as they must match the
signature of the delegate. But still, I think declaring a delegate is going
to be superfluous, unless the same function is used multiple times in a
block.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.
 
Hi Kevin,

One thing for sure is that all my method signtures would be the same.
They would all take in a string parameter and return a boolean.
So instead of using a switch statement I wanted the code to automaticaly
pick up the function.
so looping through the CSV, I would get the function name in a variable.
My idea was to assign this variable to the delegate for the function name.

So if delegate is declared as -

Public Delegate Function ValidationFunction(ByVal strToValidate As String)
As Boolean

Somewhere in code I wanted to do something like this -
Dim objFunction As ValidationFunction
methodName = objDataTable.item(1)

And then this is where the problem arises

objFunction = New ValidationFunction(AddressOf methodName)

The declaration above requires a function name and not a variale I suppose.
Am repeating all this to make sure that something like the step above cannot
be done. Is that right?

So there is no way I can call a function dynamically based on the name
stored in a variable ???

Thanks
PK
 
Hi P K,

The problem is that a function is not a string. It's an object. To get the
boject from the string, you would need to create strings that match the
function full names, and then use reflection to invoke the function, which
again, is a heck of a lot of work to do compared to using a switch
statement, both for you, and for your app. Reflection consumes a good bit of
processor to use.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.
 

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

Back
Top