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 3 991
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.
"P K" <PK@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com... 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 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
"Kevin Spencer" wrote: 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.
"P K" <PK@discussions.microsoft.com> wrote in message news:C6**********************************@microsof t.com...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,
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.
"P K" <PK@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com... 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
"Kevin Spencer" wrote:
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.
"P K" <PK@discussions.microsoft.com> wrote in message news:C6**********************************@microsof t.com... >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 > > This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: LP |
last post by:
Hello!
I am still transitioning from VB.NET to C#. I undertand the basic concepts
of Delegates, more so of Events and somewhat understand AsyncCallback
methods. But I need some clarification on...
|
by: bharathreddy |
last post by:
Delegates
Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
|
by: =?Utf-8?B?T2xkQ2FEb2c=?= |
last post by:
My question is regarding the use of delegates in C#. I see how .Net uses
delegates to wire event handlers to events. It’s an object created by a
single line of code by the system and that makes...
|
by: Siegfried Heintze |
last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I
would like some clarify the difference between events and delegates. On page
156 I see a WinForms example of timer that...
|
by: raylopez99 |
last post by:
They usually don't teach you in most textbooks I've seen that
delegates can be used to call class methods from classes that are
'unaware' of the delegate, so long as the class has the same...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |