Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Identifying input parameter names of a function at run-time

Posted on 2008-10-04
15
Medium Priority
?
379 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
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 

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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 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