Solved

Identifying input parameter names of a function at run-time

Posted on 2008-10-04
15
359 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
  • 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now