Identifying input parameter names of a function at run-time

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
PantoffelSlippersAsked:
Who is Participating?
 
RishadanPortConnect With a Mentor Commented:
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
 
RishadanPortCommented:
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
 
PantoffelSlippersAuthor Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
RishadanPortCommented:
I am a bit confused. What do you mean the "name"?
0
 
PantoffelSlippersAuthor Commented:
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
 
RishadanPortCommented:
Let me get this straight...

You declare some variable

int variable;

And you want to pass in the string "variable"?
0
 
PantoffelSlippersAuthor Commented:
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
 
PantoffelSlippersAuthor Commented:
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
 
RishadanPortCommented:
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
 
PantoffelSlippersAuthor Commented:
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
 
RishadanPortCommented:
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
 
PantoffelSlippersAuthor Commented:
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
 
PantoffelSlippersAuthor Commented:
Thanks
0
 
RishadanPortCommented:
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
 
PantoffelSlippersAuthor Commented:
RishadanPort,

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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.