Solved

Identifying input parameter names of a function at run-time

Posted on 2008-10-04
15
357 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

12 Experts available now in Live!

Get 1:1 Help Now