Solved

Identifying input parameter names of a function at run-time

Posted on 2008-10-04
15
371 Views
Last Modified: 2013-11-07
Hi Experts,

I have a very strange question today:

I'm writing a VB.Net 2005 application that extracts data from SQL Server and passes it on to a web service.   My problem is that the web service is not yet written.  I know each field that I extract will have a corresponding input parameter in the web service function I will call.  I don't know what the names or order of these variables will be though.

I was hoping I could at run-time determine what the name of each input parameter is, compare that to a map / settings file that could be manipulated by a user and as a result determine which source field in SQL Server to use.

To complete my development, I will write my own dummy web service.  If I'm able to use the map I'm talking about, all we have to do once the real web service is completed, is to change the web service variable names in the map!

For example, if I have an XML file as the map, I could specify that Var1 is populated by the FIrstName field in SQL Server, any input variable called Surname is populated by the LastName field in SQL Server etc etc.

So, if my web service looks like this:

Private Function TheWebServiceMethod(Var 1 String, Surname String, SomeVar Integer) AS Boolean
.....
End Function

When I call the web service and pass through variables, I could say that the value I'm passing through depends on what the input parameter is called.  If it's called "Var1" I will pass though the FirstName value but if it's called "Surname" I will pass thought the LastName value and if it's called "SomeVar" I will pass thought the age.

I just don't know how to determine at run-time what the input parameter is called.....

Is it possible and if so, how do I do it?

Thanks
0
Comment
Question by:PantoffelSlippers
[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
  • 8
  • 7
15 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22640236
In C#, if you don't know what a varibles type is, you can simply use the "is" operator to determine what type it is, and then type cast it to that object. Example in C#:

public void method(object someObj){
   if(object is Car){
      Car newCar = (Car)object;
   }
   else if(object is Bicycle){
      Bicycle bike = (Bicycle)object;
   }
}

I hope this gives you an idea
0
 

Author Comment

by:PantoffelSlippers
ID: 22640244
Thanks RishadanPort

No, I need to know what the name of the input parameter is that I'm about to populate!

The parameters that I will populate will almost always be a string....
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22640248
I am a bit confused. What do you mean the "name"?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:PantoffelSlippers
ID: 22640272
For example:

If the web method I'm calling has the following defintion:

Private Function TheWebServiceMethod(Var 1 String, Surname String, SomeVar Integer) AS Boolean
.....
End Function


I want to call it in such a way:

Dim ReturnCode as Boolean
Return Code = TheWebServiceMethod(PassValue(<I_Need_This>), PassValue(<I_Need_This>), PassValue(<I_Need_This>))

Private Function PassValue(ParameterName as string) AS String

Select Case Parameter Name
 Case "Var1"
    Return GetFirstName()

 Case "SurName"
    Return GetSurname()

 And so on .....

End Select

End Function


See what I mean now?

I need to get a line of code that will get me a value for <I_Need_This> in the above code.

I don't know if it's possible.....
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22640286
Let me get this straight...

You declare some variable

int variable;

And you want to pass in the string "variable"?
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 500 total points
ID: 22640298
I think I see what your trying to say now... You are trying to use the declaration of the method to pass in different variables.

So for example if the function is declared like:

public void method(int Var1)

You want to pass in some attribute using the Var1 name...

-->Answer this is not possible.
0
 

Author Comment

by:PantoffelSlippers
ID: 22640300
Hi RishadanPort,

OK, declaring SomeVar as Integer was a bad example!  I apologise for that.  I am sorry!!

The variable types are not too important to me now.  All I want to know now is if I can make my application dynamic by determing the parameter names at run-time.  Don't worry about the data types now.  Almost all of them will be string anyway.

0
 

Author Comment

by:PantoffelSlippers
ID: 22640303
Hi RishadanPort,

Regarding your comment>>>>>>>>
"I think I see what your trying to say now... You are trying to use the declaration of the method to pass in different variables. "

Yes - that is exactly what I'm trying to do.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22640308
when you declare a function, be it like this:

public void method(int x)

versus

public void method(int y)

then mean exactly the same thing, however the scope inside method 1 uses x, versus the scope inside method 2 uses y
0
 

Author Comment

by:PantoffelSlippers
ID: 22640316
Hi RishadanPort

Your answer to my question was:  "It is not possible".

I will accept that answer with perfect grades but I would like to leave the question open for a day or so to see if someone is willing to challenge your answer.

Thanks for the help!
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22640317
There is no way to do this, for a number of reasons...

One important reasons... is it can be quite dangerous for your program's runtine to depend on the variable name. One day, a person will come and look at this method... determine that they do not like this variable named this way... so they rename it..... and it breaks the program!


Whenever you are declaring any type of variable to begin with,

say, "int var"

all you are really doing is allocating memory for an int. The name "var" by the compiler is later changed to an address that refers to the location of where this int is located.
0
 

Author Comment

by:PantoffelSlippers
ID: 22640328
RishadanPort,

Thank you for the explanation.  Regarding the first point, I was planning on having a map of variable names so when chaning the variable name then we just change the MAP as well.  No break!

So, I accept then that is is not possible.  I was just hoping that since the Intellisense is able to determine the name of the variable expected at that point, by some magic, the same is possible at run-time!

Thanks again

Cheers
0
 

Author Closing Comment

by:PantoffelSlippers
ID: 31503006
Thanks
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22640337
Maybe we can discuss for a bit on how to modify your code so that it won't need the variable names of the function to work?

Your code should not need to depend on function paramenter names. I would be happy to try and help you
0
 

Author Comment

by:PantoffelSlippers
ID: 22640344
RishadanPort,

Thanks - yes I'm sure there would be workarounds ...

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

734 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